I am trying to use virtual populates as explained in this post: http://thecodebarbarian.com/mongoose-virtual-populate
When using populate
with find, there is no problem. However, populating afterwards on a document does not seem to be working
ex:
SomeModelSchema.virtual('bar', {
ref: 'OtherModel',
localField: 'otherModelId',
foreignField: 'id',
justOne: true,
});
SomeOtherModelSchema = new Schema({
foo: [SomeModelSchema]
})
// This works, result.foo[0].bar is an OtherModel document
let result = await SomeOtherModel.findOne({}).populate('foo.bar');
// This does not work, result.foo[0].bar is null
result = await SomeOtherModel.findOne({});
result = await result.populate('foo.bar').execPopulate();
Thanks for reporting, will fix for next minor release :+1:
It seems this brought a regression. I have virtual populates not being populated correctly now. I'll investigate to see what's happening exactly
Yep, populate with execPopulate was broken on nested documents, now it is broken on the first level. 4.10.0 is good, 4.10.1 isn't
SomeModelSchema.virtual('bar', {
ref: 'OtherModel',
localField: 'otherModelId',
foreignField: 'id',
});
// This works, result.bar[0] is an OtherModel document
let result = await SomeModel.findOne({}).populate('bar');
// This does not work, result.bar is null
result = await SomeModel.findOne({});
result = await result.populate('bar').execPopulate();
Got any idea @vkarpov15 ?
@SBRK opened up a separate issue #5311 :point_up: to track
Most helpful comment
Thanks for reporting, will fix for next minor release :+1: