我想使用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]
});
如果我得到这样的人物对象,则不会填充虚拟位置属性
PersonelModel.findOne({_id: 1}).populate({path: 'EmploymentRoles.Location'}).exec();
但是,如果将嵌套模式从数组更改为对象,则按以下方式工作
// Updated parent object schema change from array to object
var PersonSchema = new mongoose.Schema({
_id: mongoose.Schema.ObjectId,
EmploymentRoles = {EmploymentRoleSchema}
});
现在运行以下代码会填充位置虚拟属性
PersonelModel.findOne({_ id:1})。populate({path:' EmploymentRoles.Location'})。exec();
填充虚拟对数组属性不起作用吗?
以下文章似乎表明它应该有效 -
http://thecodebarbarian.com/mongoose-virtual-populate.html
http://mongoosejs.com/docs/populate.html(填充虚拟部分)
谢谢,