Khazar Amjer
Khazar Amjer

Reputation: 77

Mongodb recursive search for array of objects

I have a tree structure look like this

{ 
    "_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
        }
    ]
}

the fid is a folder id and the ch is the children of folder , so I want to do a recursive search to get tree of the folders and files. In my case I have a used $graphLookup to do a recursive search but as a result I am getting other folders too

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)

My project based on Golang.

Upvotes: 1

Views: 1049

Answers (1)

Yur Gasparyan
Yur Gasparyan

Reputation: 714

I think you need to use $unwind for first, than $graphLookup, so you need to do a recursive search look like this

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)

So as a result you will get the id of the root folder and the ids of children

Upvotes: 1

Related Questions