Mongoosejs virtual populate Mongoosejs virtual populate mongoose mongoose

Mongoosejs virtual populate


Figured out what the problem was.By default, the virtual fields are not included in the output.After adding this in circle schema:

circleSchema.virtual('caregiver_details',{    ref: 'caregiver',    localField: 'caregivers',    foreignField: 'userId'});circleSchema.set('toObject', { virtuals: true });circleSchema.set('toJSON', { virtuals: true });

It now works perfectly.


If you use expressJs res.json method it's ehough to add only:

yourSchema.set('toJSON', { virtuals: true });

Or you can directly use toJSON/toObject:

doc.toObject({ virtuals: true })) // or doc.toJSON({ virtuals: true }))