猫鼬填充包含引用的对象数组 [英] Mongoose populate with array of objects containing ref
问题描述
我有一个Mongoose模式,其中包含对象的数组lists
,该对象数组包含对另一个集合的引用和一个嵌套的数字数组:
I have a Mongoose schema with an array lists
of objects that consist of a reference to another collection and a nested array of numbers:
var Schema, exports, mongoose, schema;
mongoose = require("mongoose");
Schema = mongoose.Schema;
schema = new Schema({
name: {
type: String,
required: true,
unique: true,
trim: true
},
lists: [
{
list: {
type: Schema.ObjectId,
require: true,
ref: "List"
},
allocations: [
{
type: Number,
required: true
}
]
}
],
createdAt: {
type: Date,
"default": Date.now
},
updatedAt: {
type: Date
}
});
exports = module.exports = mongoose.model("Portfolio", schema);
但是,如果没有TypeError: Cannot read property 'ref' of undefined
,我将无法使populate
正常工作.我已经尝试过populate('list')
和populate('lists list')
,但是我没有正确地调用事物,或者我的模式没有正确地形成.如果我自己单独引用列表,就不会有这个问题:
However, I cannot get populate
to work as expected without getting a TypeError: Cannot read property 'ref' of undefined
. I've tried populate('list')
and populate('lists list')
but I'm either not calling things correctly or my Schema isn't formed correctly. I don't have this problem if I simply reference the lists by themselves:
lists: [
{
type: Schema.ObjectId,
require: true,
ref: "List"
}
]
但是我想在每个列表旁边都有分配数组.我需要做些什么才能得到想要的行为?
but I want to have the allocations array alongside each list. What do I need to do to get the behavior I want?
推荐答案
我找到了答案:populate('lists.list')
有效.感谢这个问题:猫鼬填充在一个对象内吗?
I found the answer: populate('lists.list')
works. Thanks to this question: Mongoose populate within an object?
这篇关于猫鼬填充包含引用的对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!