今天我读了一下$ graphLookup功能,我对这个(对我来说)新功能印象深刻。 目前我用eval解决了这些类型的问题,现在我将这些函数转移到$ graphLookup。
其中一个功能是“getAllSubFolders()”。现在我遇到了一些麻烦,因为我的 connectFromField是一个对象,而不是一个数组(如mongoDB文档示例中所示)。
更好地解读:
文件夹文档的结构如下:
{
"_id" : ObjectId("58286a66d43ee415567ab4c8"),
"Children" : {
"File_58286ce0d43ee415567ab4e5" : "58286ce0d43ee415567ab4e5",
"Folder_5829d284d43ee41fa74f7e50" : "5829d284d43ee41fa74f7e50",
},
"UserID": "1"
...
}
现在正如我所提到的,我需要一个聚合管道(使用$ 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"
}
}
]);
PS:很好但不需要的是我们只搜索在密钥中有Folder_的孩子。我不知道是否有可能过滤它。
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("581bc233d43ee401131d82b8"),
"Children" : {
"Folder_5826034c0c0a1c005d64f921" : "5826034c0c0a1c005d64f921"
},
},
{
"_id" : ObjectId("5826034c0c0a1c005d64f921"),
"Children" : {
"Folder_593032743f54cc0078025802" : "593032743f54cc0078025802"
}
},