猫鼬虚拟填充数组

dal*_*jit 5 mongoose

我想使用 mongoose 虚拟填充来获取数据,但对于嵌套数组,但没有按预期工作 -

以下是我的架构 -

// schema for organisation structure
var OrganisationStructureSchema = new mongoose.Schema({
    _id: { type: Schema.ObjectId },
    Description: { type: String }        
});

// First nested array schema
var EmploymentRoleSchema = new mongoose.Schema({
        _id: {type: mongoose.Schema.ObjectId,
        LocationId: { type: Schema.ObjectId }            
});

// Create the virtual to populate
EmploymentRoleSchema.virtual('Location', {
  ref: 'OrganisationStructure',
  localField: 'LocationId',
  foreignField: '_id'
});

// Parent object schema
var PersonSchema = new mongoose.Schema({
   _id: mongoose.Schema.ObjectId,
   EmploymentRoles = [EmploymentRoleSchema]
});
Run Code Online (Sandbox Code Playgroud)

如果我得到这样的 person 对象,它不会填充虚拟位置属性

PersonelModel.findOne({_id: 1}).populate({path:    'EmploymentRoles.Location'}).exec();
Run Code Online (Sandbox Code Playgroud)

但是,如果将嵌套模式从数组更改为对象,如下所示

// Updated parent object schema change from array to object
var PersonSchema = new mongoose.Schema({
   _id: mongoose.Schema.ObjectId,
   EmploymentRoles = {EmploymentRoleSchema}
});
Run Code Online (Sandbox Code Playgroud)

现在运行以下代码填充位置虚拟属性

PersonelModel.findOne({_id: 1}).populate({path: 'EmploymentRoles.Location'}).exec();

填充虚拟是否不适用于数组属性?

以下文章似乎表明它应该有效-

http://thecodebarbarian.com/mongoose-virtual-populate.html

http://mongoosejs.com/docs/populate.html(填充虚拟部分)

谢谢,