mongodb $ lookup 3级嵌套文档

时间:2018-10-16 08:47:08

标签: mongodb mongodb-query aggregation-framework

我是mongo的新手,并且在以下方面苦苦挣扎。 在我的mongodb数据库中,有3个集合,结构如下。

lv1:

{
    "_id": ObjectId("58650f1abbf1cd8804d0abde"), 
    "name": "lv1_aaa"
}

lv2:

{
    "_id": ObjectId("5ba45de41e78c7eb3fdfbfa6"), 
    "lv1_id": ObjectId("58650f1abbf1cd8804d0abde"), 
    "name": "lv2_bbb"
}

lv3:

{
    "_id": ObjectId("5ba45de41e78c7eb3fdfbfa6"), 
    "lv1_id": ObjectId("58650f1abbf1cd8804d0abde"),
    "lv2_id": ObjectId("58d8c3e1bbf1cd7436117bd6"), 
    "name": "lv3_ccc"
}

如何在下面使用$ lookup获取数据结构

[
    {
        "_id": ObjectId("58650f1abbf1cd8804d0abde"), 
        "name": "lv1_aaa",
        "children": [
            {
                "_id": ObjectId("5ba45de41e78c7eb3fdfbfa6"), 
                "lv1_id": ObjectId("58650f1abbf1cd8804d0abde"), 
                "name": "lv2_bbb",
                "children": [
                    {
                        "_id": ObjectId("5ba45de41e78c7eb3fdfbfa6"), 
                        "lv1_id": ObjectId("58650f1abbf1cd8804d0abde"),
                        "lv2_id": ObjectId("58d8c3e1bbf1cd7436117bd6"), 
                        "name": "lv3_ccc"
                    },
                    ......
                ]
            },
            ......
        ]
    },
    ......
]

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

您可以在mongodb 3.6 及更高版本

下尝试以下聚合
public CordovaWebViewImpl(CordovaWebViewEngine cordovaWebViewEngine) {
    if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
        this.engine = cordovaWebViewEngine; // here xwalk view
    } else {
        this.engine = cordovaWebViewEngine; // here system web view engine
    }
}