Mongodb graphLookup StartWith对象而不是数组
今天我读了一篇关于$graphLookup特性的文章,我对这个(对我来说)新特性印象深刻。 现在我用eval解决了这类问题,现在我要把这些函数转移到$graphLookup 其中一个函数是“getAllSubFolders()”。现在我遇到了一些麻烦,因为我的 connectFromField是一个对象,而不是数组(如mongoDB文档示例中所示) 要更好地抵御这种情况,请执行以下操作: 文件夹文档的结构如下所示:Mongodb graphLookup StartWith对象而不是数组,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,今天我读了一篇关于$graphLookup特性的文章,我对这个(对我来说)新特性印象深刻。 现在我用eval解决了这类问题,现在我要把这些函数转移到$graphLookup 其中一个函数是“getAllSubFolders()”。现在我遇到了一些麻烦,因为我的 connectFromField是一个对象,而不是数组(如mongoDB文档示例中所示) 要更好地抵御这种情况,请执行以下操作: 文件夹文档的结构如下所示: { "_id" : ObjectId("58286a66d43ee415
{
"_id" : ObjectId("58286a66d43ee415567ab4c8"),
"Children" : {
"File_58286ce0d43ee415567ab4e5" : "58286ce0d43ee415567ab4e5",
"Folder_5829d284d43ee41fa74f7e50" : "5829d284d43ee41fa74f7e50",
},
"UserID": "1"
...
}
{
"_id" : ObjectId("581bc233d43ee401131d82b8"),
"Children" : {
"Folder_5826034c0c0a1c005d64f921" : "5826034c0c0a1c005d64f921"
},
},
{
"_id" : ObjectId("5826034c0c0a1c005d64f921"),
"Children" : {
"Folder_593032743f54cc0078025802" : "593032743f54cc0078025802"
}
},
现在正如我提到的,我需要一个聚合管道(带有$graphLookup?),它将返回所有子文件夹。
有什么办法解决这个问题吗
更具体地说:我目前的问题是startWith。我需要像这样的东西:
db.folders.aggregate( [
{ $match : { "UserID" : "1" } },
{
$graphLookup: {
from: "folders",
startWith: "getObjectValues($Children)", //returns an array with the object values
connectFromField: "getObjectValues($Children)", // not sure about this
connectToField: "_id",
as: "sub_folders"
}
}
]);
附言:如果我们只搜索键中有文件夹的子项,这很好,但不是必需的。我不知道是否有可能过滤它
Ok, so our origin document looks like this:
{
"_id" : ObjectId("581bae37d43ee401131d8293"),
"UserID" : "37",
"Children" : {
"File_581bb4abd43ee401131d82a0" : "581bb4abd43ee401131d82a0",
"Folder_581bc233d43ee401131d82b8" : "581bc233d43ee401131d82b8",
...
}
}
Now we need an query that return this document:
{
"_id" : ObjectId("581bc233d43ee401131d82b8"),
"Children" : {
"Folder_5826034c0c0a1c005d64f921" : "5826034c0c0a1c005d64f921"
},
}
but also the child folder with the id 5826034c0c0a1c005d64f921.
{
"_id" : ObjectId("5826034c0c0a1c005d64f921"),
"Children" : {
"Folder_593032743f54cc0078025802" : "593032743f54cc0078025802"
}
}
... recursive, down to the last element (until the object children contains no more folders)
总的来说,结果应该是这样的:
{
"_id" : ObjectId("58286a66d43ee415567ab4c8"),
"Children" : {
"File_58286ce0d43ee415567ab4e5" : "58286ce0d43ee415567ab4e5",
"Folder_5829d284d43ee41fa74f7e50" : "5829d284d43ee41fa74f7e50",
},
"UserID": "1"
...
}
{
"_id" : ObjectId("581bc233d43ee401131d82b8"),
"Children" : {
"Folder_5826034c0c0a1c005d64f921" : "5826034c0c0a1c005d64f921"
},
},
{
"_id" : ObjectId("5826034c0c0a1c005d64f921"),
"Children" : {
"Folder_593032743f54cc0078025802" : "593032743f54cc0078025802"
}
},
您真的想要
$graphLookup
?也许你最好解释一下你想做什么。管道阶段真正做的就是遍历一棵树,并在祖先文档的结果数组中显示父层次结构。如果你的目标是让所有的父母都能从祖先那里看到,那么这就是你的目标。如果你正试图建立一个“树状视图”,那么可能是也可能不是。这取决于您对如何处理它的概念。我的主要问题是eval处于“弃用”状态,我需要一种从文件夹中获取所有子文件夹的方法。它是一个文件系统。我的最终目标是获取所有子文件夹中的所有文件($inquery)和文件大小的$sum我认为您应该显示文档和所需的结果,而不是假定使用哪种方法。当然不是eval()
。显示更多文档详细信息,减少询问特定方法的使用。感谢您的帮助Neil。我更新了问题好的,所以预期的结果是有意义的,但是应该产生该结果的文档是什么?也许你们应该简单地阅读一下,并理解为了“解决”一些事情,我们需要能够“重新提出和验证”。