Node.js mongodb-文件夹树层次结构
结构中的每个文件夹都有一个祖先id列表。 当我将文件夹移动到新的父文件夹时,我必须用新的父文件夹更新所有子文件夹:Node.js mongodb-文件夹树层次结构,node.js,mongodb,hierarchy,Node.js,Mongodb,Hierarchy,结构中的每个文件夹都有一个祖先id列表。 当我将文件夹移动到新的父文件夹时,我必须用新的父文件夹更新所有子文件夹: // root folder { id: 1, name: "folder 1", ancestorIds: [] } // child of folder 1 { id: 2 name: "folder 2", ancestorIds: [1] } // child
// root folder
{
id: 1,
name: "folder 1",
ancestorIds: []
}
// child of folder 1
{
id: 2
name: "folder 2",
ancestorIds: [1]
}
// child of folder 2
{
id: 3
name: "folder 3",
ancestorIds: [1, 2]
}
// other root folder
{
id: 4,
name: "folder 4",
ancestorIds: []
}
现在我想将“文件夹2”(id 2)移动到另一个根文件夹(文件夹4,id 4)中。
结构应如下所示:
// root folder
{
id: 1,
name: "folder 1",
ancestorIds: []
}
// other root folder
{
id: 4,
name: "folder 4",
ancestorIds: []
}
// now child of folder 4
{
id: 2
name: "folder 2",
ancestorIds: [4]
}
// still child of folder 2, but ancestor changed!
{
id: 3
name: "folder 3",
ancestorIds: [4, 2]
}
要正确修改子祖先列表(id 3),我必须删除旧祖先(id 1)并添加新祖先(id 4)。
在nodejs中,它将如下所示:
collection.update({ "ancestorIds": 2 }, { "$pullAll": { "ancestorIds": [1] } }, { multi: true }, function (err) {
if (err) throw err;
collection.update({ "ancestorIds": 2 }, { "$pushAll": { "ancestorIds": [4] } }, { multi: true }, function (err) {
if (err) {
// critical error here
throw err;
}
});
});
如果拉动请求成功,但推送失败:
AnceStorId现在没有根文件夹,列表无效!
[2] 而不是[4,2]或[1,2]
我可以在单个db请求中修改anchestor ID吗?
还是有一个根本不同的解决方案?首先,pushAll不受欢迎:。MongoDB树结构信息