Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb对对象数组的递归搜索_Mongodb_Go_Aggregation Framework - Fatal编程技术网

Mongodb对对象数组的递归搜索

Mongodb对对象数组的递归搜索,mongodb,go,aggregation-framework,Mongodb,Go,Aggregation Framework,我有一个这样的树形结构 { "_id" : ObjectId("59aebe21f002a8556ca78310"), "fid" : ObjectId("59aebe216b96002252a89d7b"), "pr" : [ ], "ch" : [ { "_id" : ObjectId("59aebe326b96002252a89d7d"), "trashed" : false

我有一个这样的树形结构

{ 
    "_id" : ObjectId("59aebe21f002a8556ca78310"), 
    "fid" : ObjectId("59aebe216b96002252a89d7b"), 
    "pr" : [

    ], 
    "ch" : [
        {
            "_id" : ObjectId("59aebe326b96002252a89d7d"), 
            "trashed" : false
        }, 
        {
            "_id" : ObjectId("59aebe376b96002252a89d7f"), 
            "trashed" : false
        }
    ]
}
{ 
    "_id" : ObjectId("59aebe33f002a8556ca78347"), 
    "fid" : ObjectId("59aebe326b96002252a89d7d"), 
    "pr" : [
        {
            "_id" : ObjectId("59aebe216b96002252a89d7b"), 
            "trashed" : false
        }
    ], 
    "ch" : [
        {
            "_id" : ObjectId("59aebe3b6b96002252a89d81"), 
            "trashed" : false
        }
    ]
}
fid是文件夹id,ch是文件夹的子项,因此我想进行递归搜索以获得文件夹和文件的树。 在我的例子中,我使用了一个
$graphLookup
进行递归搜索,但结果我也得到了其他文件夹

pipeline := []bson.M{
        {"$match": bson.M{"fid": id}},
        {"$graphLookup": bson.M{
            "from":             "tree",
            "startWith":        "$fid",
            "connectFromField": "fid",
            "connectToField":   "ch._id",
            "as":               "parents",
        }},
        {"$match": bson.M{"ch.trashed": false}},
    }

    Connection.Session.DB("cctv_storage").C("tree").Pipe(pipeline).All(&tData)

我的项目基于Golang。

我认为首先需要使用
$unwind
,而不是
$graphLookup
,因此需要执行如下递归搜索

var tData struct {
        Id    bson.ObjectId     `bson:"_id"`
        Child [][]bson.ObjectId `bson:"child"`
    }

pipeline := []bson.M{
        {"$unwind": bson.M{
            "path": "$pr",
            "preserveNullAndEmptyArrays": true,
        }},
        {"$graphLookup": bson.M{
            "from":             "tree",
            "startWith":        "$fid",
            "connectFromField": "fid",
            "connectToField":   "pr._id",
            "as":               "child",
        }},
        {"$match": bson.M{"fid": id}},
        {"$group": bson.M{"_id": id, "child": bson.M{"$addToSet": "$child.fid"}}},
    }
Connection.Session.DB("cctv_storage").C("tree").Pipe(pipeline).One(&tData)

因此,您将获得根文件夹的id子文件夹的id

请阅读
$graphLookup
的工作原理:如果
“connectToField”:“ch.\u id”
那么
as
应该是“孩子”,而不是“家长”。第二个
$match
仅过滤顶级文档,而不是所有子/父文档。此外,示例中的所有文档都有
“ch.trashed”:false