dragon88112 2017-09-06 08:03
浏览 152

Mongodb递归搜索对象数组

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.

  • 写回答

1条回答 默认 最新

  • dongshao6591 2017-09-06 08:50
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体