Mongodb 填充内部填充猫鼬
我想创建一个内部填充 我的架构看起来像Mongodb 填充内部填充猫鼬,mongodb,mongoose,Mongodb,Mongoose,我想创建一个内部填充 我的架构看起来像 //item schema item { name : String, type : { type: mongoose.Schema.Types.ObjectId, ref: 'item_types' }, box : { type: mongoose.Schema.Types.ObjectId, ref: 'box' }, } //box schema box{ name : Str
//item schema
item {
name : String,
type : { type: mongoose.Schema.Types.ObjectId, ref: 'item_types' },
box : { type: mongoose.Schema.Types.ObjectId, ref: 'box' },
}
//box schema
box{
name : String,
department : [{ type: mongoose.Schema.Types.ObjectId, ref: 'departments' }],
type : [{ type: mongoose.Schema.Types.ObjectId, ref: 'types' }]
manufacturer : { type: mongoose.Schema.Types.ObjectId, ref: 'manufacturers' },
}
//departments schema
departments{
name : String
}
//types schema
types{
name : String
}
//item_types schema
item_types{
name : String
}
当我打电话的时候
item
.find()
.populate('type ', 'name')
.populate('box ', 'name')
.exec(function (err, item) {}
它工作得很好。但是,当我这样调用时,它并没有填充数据,只是显示ID而已
item
.find()
.populate('type ', 'name')
.populate('box ', 'name')
.populate('box.department ', 'name')
.exec(function (err, item) {}
如何做到这一点
提前谢谢
Babji我强烈推荐Mongoose插件“Mongoose deep populate”。我在很多项目中使用它,它可以轻松地处理这些问题 使用示例和文档: