[英]Issue populating child collections using MongoDB and Mongoose
我有以下架构:
const MenuSchema = new mongoose.Schema({
name: String,
type: String,
children: [{ type: ObjectId, ref: 'Menu' }],
});
以及以下查询:
const item = await Menu.findOne({ _id: <id> }).populate({
path: 'children',
});
console.log(item.children);
我试图用实际的相关文档递归地填充子数组,我可以填充第一个集合,但我需要有效地递归地执行此操作。
我想你可以做
const item = await Menu.findOne({ _id: <id> }).populate('children');
console.log(item.children);
.populate 将路径作为第一个参数,无需输入 object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.