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