简体   繁体   English

MongoDb聚合查找嵌套数组

[英]MongoDb Aggregation lookup nested Array

Lets say I have a MongoDB collection with docs like: 可以说我有一个MongoDB集合,其中包含以下文档:

{
    _id,
    name: "someName",
    children:[
        ObjectId,
        ObjectId,
    ]
}

Is it possible to to a deep nest with child using the aggregation framework and lookup? 是否可以使用聚合框架和查找与子对象进行深度嵌套? Another problem is that we do not often know what the lookup field of the child is going to be, therefore the child has a "Lookup" array with the fields that should be looked up in the database. 另一个问题是,我们不经常知道子项的查找字段是什么,因此,子项具有“查找”数组,其中包含应在数据库中查找的字段。

So for example, the result would be with a depth of 3: 因此,例如,结果的深度为3:

{
    _id,
    name: "someName",
    children:[
        {
            _id,
            name: "someName",
            children:[
                {
                    _id,
                    name: "someName",
                    children:[
                        ObjectId,
                        ObjectId,
                    ]
                }
                {
                    _id,
                    name: "someName",
                    children:[
                        ObjectId,
                        ObjectId,
                    ]
                }
            ]
        },
        {
            _id,
            name: "someName",
            children:[
                ObjectId,
                ObjectId,
            ]
        }
    ]
}

I would like to be able to create a function that enables me to specify the "depth" of the lookup. 我希望能够创建一个使我能够指定查找“深度”的函数。

We are using the mongodb NodeJs driver. 我们正在使用mongodb NodeJs驱动程序。

I see 2 solutions here: 我在这里看到2种解决方案:

  1. To change your schema and try to use nested sets pattern. 要更改架构并尝试使用嵌套集模式。 See https://docs.mongodb.com/manual/tutorial/model-tree-structures-with-nested-sets/ 参见https://docs.mongodb.com/manual/tutorial/model-tree-structures-with-nested-sets/

  2. To make a recursive function to fetch all data you need, but you will face a problem such as N-aggregation method calls 要创建一个递归函数以获取所需的所有数据,但您将遇到诸如N聚合方法调用之类的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM