mongoose-mpath 模块正在创建文档但未分配给它的父级
mongoose-mpath module is creating the document but not assigned to it's parent
const {category, parent} = newCategory;
const newCat = new Category({
_id: new mongoose.Types.ObjectId(),
name: category,
parent: parent
}).save((err, res) => {
if(err) { console.log(err)}
else{
console.log("res in create category:", res);
// io.to(user.room).emit('create-category-to-list', {res});
}
})
日志中
res in create category: {
codewords: [],
children: [],
_id: 604916cf866a154e284b2e29,
name: 'nameofasdasdasdcat',
parent: 60490c9ce00e8b7a38cf4752,
path: '60490c9ce00e8b7a38cf4752#604916cf866a154e284b2e29',
_v:0
}
此处创建了文档但未分配给其父级,这是发生这种情况的原因。
这里可以看到parent children数组是空的
root: {
children: [],
_id: 60490c9ce00e8b7a38cf4752,
name: 'root',
path: '60490c9ce00e8b7a38cf4752',
_v:0
}
我在这里使用 mongoose pulgin(mongoose-mpath) 创建树形数据。
对于 mongoose-mpath https://www.npmjs.com/package/mongoose-mpath
在 getChildrenTree 函数中需要将选项传递为 lean:false
const parent = await Folder.findOne({ _id: user.rootId });
const tree = await parent.getChildrenTree(
{
populate: 'codewords',
options: { lean: false } //you need to pass options as lean: false
});
console.log({ tree });
在日志中
{
tree: [
{
codewords: [],
children: [],
_id: 604c6c5bc8a2e03ee43c4c7b,
name: 'Category-name-2',
parent: 604c690b87924705a401f9ce,
path: '604c690b87924705a401f9ce#604c6c5bc8a2e03ee43c4c7b',
__v: 0
},
{
codewords: [],
children: [],
_id: 604c6ce2c8a2e03ee43c4c7d,
name: 'Category-name-2',
parent: 604c690b87924705a401f9ce,
path: '604c690b87924705a401f9ce#604c6ce2c8a2e03ee43c4c7d',
__v: 0
},
{
codewords: [],
children: [Array],
_id: 604c6d21c8a2e03ee43c4c7e,
name: 'Category-name-3',
parent: 604c690b87924705a401f9ce,
path: '604c690b87924705a401f9ce#604c6d21c8a2e03ee43c4c7e',
__v: 0
}
]
}
为了更好地理解,您可以查看 mongoose-mpath github 问题部分 ==>
https://github.com/vikpe/mongoose-mpath/issues/10
const {category, parent} = newCategory;
const newCat = new Category({
_id: new mongoose.Types.ObjectId(),
name: category,
parent: parent
}).save((err, res) => {
if(err) { console.log(err)}
else{
console.log("res in create category:", res);
// io.to(user.room).emit('create-category-to-list', {res});
}
})
日志中
res in create category: {
codewords: [],
children: [],
_id: 604916cf866a154e284b2e29,
name: 'nameofasdasdasdcat',
parent: 60490c9ce00e8b7a38cf4752,
path: '60490c9ce00e8b7a38cf4752#604916cf866a154e284b2e29',
_v:0
}
此处创建了文档但未分配给其父级,这是发生这种情况的原因。
这里可以看到parent children数组是空的
root: {
children: [],
_id: 60490c9ce00e8b7a38cf4752,
name: 'root',
path: '60490c9ce00e8b7a38cf4752',
_v:0
}
我在这里使用 mongoose pulgin(mongoose-mpath) 创建树形数据。
对于 mongoose-mpath https://www.npmjs.com/package/mongoose-mpath
在 getChildrenTree 函数中需要将选项传递为 lean:false
const parent = await Folder.findOne({ _id: user.rootId });
const tree = await parent.getChildrenTree(
{
populate: 'codewords',
options: { lean: false } //you need to pass options as lean: false
});
console.log({ tree });
在日志中
{
tree: [
{
codewords: [],
children: [],
_id: 604c6c5bc8a2e03ee43c4c7b,
name: 'Category-name-2',
parent: 604c690b87924705a401f9ce,
path: '604c690b87924705a401f9ce#604c6c5bc8a2e03ee43c4c7b',
__v: 0
},
{
codewords: [],
children: [],
_id: 604c6ce2c8a2e03ee43c4c7d,
name: 'Category-name-2',
parent: 604c690b87924705a401f9ce,
path: '604c690b87924705a401f9ce#604c6ce2c8a2e03ee43c4c7d',
__v: 0
},
{
codewords: [],
children: [Array],
_id: 604c6d21c8a2e03ee43c4c7e,
name: 'Category-name-3',
parent: 604c690b87924705a401f9ce,
path: '604c690b87924705a401f9ce#604c6d21c8a2e03ee43c4c7e',
__v: 0
}
]
}
为了更好地理解,您可以查看 mongoose-mpath github 问题部分 ==> https://github.com/vikpe/mongoose-mpath/issues/10