Mongodb recursive query

谁说我不能喝 提交于 2019-12-04 19:31:39

问题


I have the following schema in my taxon collection :

{ 
  "_id": 1, 
  "na": [ "root_1",
        "root_2",
        "root_3" ], 
  "pa": 1 
},{
  "_id": 2, 
  "na": [ "name_1", 
        "name_2", 
        "name_3"], 
  "pa": 1
},{
  "_id": 4, 
  "na": [ "otherName_1", 
        "otherName_2", 
        "otherName_3"],
  "pa": 2
}

Each document is related to another by the parent field, which correspond to the _id of it's parent.

I would like to perform a recursive search to get the following result:

{ "_id": 4, 
  "nameList": [ "otherName_1",
              "name_1",
              "root_1"]
} 

From document with a certain _id, get the first item of na array of each parent until document with _id: 1 is reached

I currently get this result by performing X queries (one by parent document, here 3 for example), but I'm pretty sure that this can be achieved using a single query. I already looked at the new $graphLookup operator, but couldn't manage to get my way with it...

Is it possible to achieve this in a single query using MongoDB 3.4.1?

Edit

I would run this for 50 documents each time, so the optimal solution would be to combine everything in a single query

for example, it would looks like

var listId = [ 4, 128, 553, 2728, ...];
var cursor = db.taxon.aggregate([
  {$match: 
     { _id: {$in: listId}}
  }, ...
)];  

and would output :

[{ "_id": 4, 
  "nameList": [ "otherName_1",
              "name_1",
              "root_1"]
}, { "_id": 128, 
  "nameList": [ "some_other_ame_1",
              "some_name_1",
              "root_1"]
}, { "_id": 553, 
  "nameList": [ "last_other_ame_1",
              "last_name_1",
              "root_1"]
} ... ]

try it online: mongoplayground.net/p/Gfp-L03Ub0Y


回答1:


You can try below aggregation.

Stages $match - $graphLookup - $project.

$reduce to pick the first element from the each of $graphLookup nameList's na array.

db.taxon.aggregate([{
    $match: {
        _id: {
            $in: listId
        }
    }
}, {
    $graphLookup: {
        from: "taxon",
        startWith: "$_id",
        connectFromField: "pa",
        connectToField: "_id",
        as: "nameList"
    }
}, {
    $project: {
        nameList: {
            $reduce: {
                input: "$nameList",
                initialValue: [],
                in: {
                    "$concatArrays": ["$$value", {
                        $slice: ["$$this.na", 1]
                    }]
                }
            }
        }
    }
}])


来源:https://stackoverflow.com/questions/42787293/mongodb-recursive-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!