我想使用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]
});
If I get person object like this it does not populate the virtual location property
PersonelModel.findOne({_id: 1}).populate({path: 'EmploymentRoles.Location'}).exec();
But if change nested schema from array to object as follows it works
// Updated parent object schema change from array to object
var PersonSchema = new mongoose.Schema({
_id: mongoose.Schema.ObjectId,
EmploymentRoles = {EmploymentRoleSchema}
});
Now on running following code populate the location virtual property
PersonelModel.findOne({_ id:1}) . populate({path:'EmploymentRoles.Location'}) . exec();
填充虚拟不适用于数组属性吗?
以下文章似乎表明它应该工作 -
http://thecodebarbarian.com/mongoose-virtual-populate.html
http://mongoosejs.com/docs/populate.html(填充虚拟部分)
谢谢,