Javascript 使用MongoDB和Mongoose填充子集合的问题
我有以下模式:Javascript 使用MongoDB和Mongoose填充子集合的问题,javascript,mongodb,mongoose,Javascript,Mongodb,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', }); con
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=wait Menu.findOne({u id:})。填充({
路径:“儿童”,
});
console.log(item.children);
我试图用实际的相关文档递归地填充子数组,我可以填充第一个集合,但我需要有效地递归地填充。我想你可以这样做
const item=wait Menu.findOne({u id:}).populate('children');
console.log(item.children);
.populate将路径作为第一个参数,无需放入对象