$ lookup加入嵌套数组元素

时间:2018-06-19 12:44:09

标签: mongodb mongodb-query aggregation-framework

我有两个收藏集SystemFeaturesSystemRoleFeatures

SystemFeatures模式具有以下记录

{
    "_id" : ObjectId("5a97af5ec442b59373b8dff3"),
    "Name" : "Manage Operation",
    "Description" : "Manage the Operations Details.",
    "ChildFeatures" : [{
            "_id" : ObjectId("5a97b084c442b59373b8e004"),
            "Name" : "Add Operation",
            "Description" : "Add Operation",
            "ParentId" : ObjectId("5a97af5ec442b59373b8dff3")
        }, {
            "_id" : ObjectId("5a97b084c442b59373b8e005"),
            "Name" : "Edit Operation",
            "Description" : "Edit Operation",
            "ParentId" : ObjectId("5a97af5ec442b59373b8dff3")
        }
    ]
}{
    "_id" : ObjectId("5a97af5ec442b59373b8dff4"),
    "Name" : "Manage User Roles",
    "Description" : "Manage the Functional Roles Details.",
    "ChildFeatures" : [{
            "_id" : ObjectId("5a97b084c442b59373b8e006"),
            "Name" : "Add role",
            "Description" : "Add role",
            "ParentId" : ObjectId("5a97af5ec442b59373b8dff4")
        }, {
            "_id" : ObjectId("5a97b084c442b59373b8e007"),
            "Name" : "Edit role",
            "Description" : "Edit role",
            "ParentId" : ObjectId("5a97af5ec442b59373b8dff4")
        }
    ]
}{..}

SystemRoleFeatures模式具有以下记录

    {
        "_id" : ObjectId("5a9fd2d0b1c4f30e432fd5e6"),
        "SystemRoleId" : ObjectId("5a9f9583e8c0dc6b0c5456c9"),
        "SystemFeatures" : [{
                "SystemFeatureId" : ObjectId("5a97af5ec442b59373b8dff3"),
                "Isdefault" : true,
                "ChildFeatures" : [{
                        "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e004"),
                        "Isdefault" : true
                    }, {
                        "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e005"),
                        "Isdefault" : true
                    }
                ]
            }, {
                "SystemFeatureId" : ObjectId("5a97af5ec442b59373b8dff4"),
                "Isdefault" : true,
                "ChildFeatures" : [{
                        "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e006"),
                        "Isdefault" : true
                    }, {
                        "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e007"),
                        "Isdefault" : true
                    }
                ]
            }, {
                "SystemFeatureId" : ObjectId("5a97af5ec442b59373b8dff5"),
                "Isdefault" : true,
                "ChildFeatures" : [{
                        "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e008"),
                        "Isdefault" : true
                    }, {
                        "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e009"),
                        "Isdefault" : true
                    }
                ]
            }, {
                "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e018"),
                "Isdefault" : true,
                "ChildFeatures" : []
            }
        ]
    }

我希望输出如下所示

{
        "_id" : ObjectId("5a9fd2d0b1c4f30e432fd5e6"),
        "SystemRoleId" : ObjectId("5a9f9583e8c0dc6b0c5456c9"), // i'm passing this systemroleid based on this this aggreration is done
        "SystemFeatures" : [{
                "SystemFeatureId" : ObjectId("5a97af5ec442b59373b8dff3"),
                "Name" : "Manage Operation",
                "Description" : "Manage the Operations Details.",
                "Isdefault" : true,
                "ParentId" : null, // if there is no parentId than it should be null
                "ChildFeatures" : [{
                        "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e004"),
                        "Name" : "Add Operation", //these detail from systemfeature
                        "Description" : "Add Operation",
                        "ParentId" : ObjectId("5a97af5ec442b59373b8dff3")
                        "Isdefault" : true
                    }, {
                        "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e005"),
                        "Name" : "Edit Operation",
                        "Description" : "Edit Operation",
                        "ParentId" : ObjectId("5a97af5ec442b59373b8dff3")
                        "Isdefault" : true,
                    }
                ]
            }, {
                "SystemFeatureId" : ObjectId("5a97af5ec442b59373b8dff4"),
                "Isdefault" : true,
                "Name" : "Manage User Roles",
                "Description" : "Manage the Functional Roles Details.",
                "ChildFeatures" : [{
                        "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e006"),
                        "Name" : "Add role",
                        "Description" : "Add role",
                        "ParentId" : ObjectId("5a97af5ec442b59373b8dff4")
                        "Isdefault" : true
                    }, {
                        "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e007"),
                        "Name" : "Edit role",
                        "Description" : "Edit role",
                        "ParentId" : ObjectId("5a97af5ec442b59373b8dff4")
                        "Isdefault" : true
                    }
                ]
            }, {
                "SystemFeatureId" : ObjectId("5a97af5ec442b59373b8dff5"),
                // same from systemfeature document
                "Isdefault" : true,
                "ChildFeatures" : [{
                        "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e008"),
                        // same from systemfeature document
                        "Isdefault" : true
                    }, {
                        "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e009"),
                        // same from systemfeature document
                        "Isdefault" : true
                    }
                ]
            }, {
                "SystemFeatureId" : ObjectId("5a97b084c442b59373b8e018"),
                // same from systemfeature document
                "Isdefault" : true,
                "ChildFeatures" : []
            }
        ]
    }

为此,我正在做这些事情,

db.SystemRoleFeatures.aggregate([{
                "$match" : {
                    "SystemRoleId" : ObjectId("5a9f9583e8c0dc6b0c5456c9")
                }
            }, {
                "$addFields" : {
                    "join" : {
                        "$reduce" : {
                            "input" : "$SystemFeatures.ChildFeatures.SystemFeatureId",
                            "initialValue" : [],
                            "in" : {
                                "$concatArrays" : ["$$value", "$$this"]
                            }
                        }
                    }
                }
            }, {
                "$lookup" : {
                    "from" : "SystemFeatures",
                    "localField" : "join",
                    "foreignField" : "_id",
                    "as" : "join"
                }
            }
        ]).pretty()

但是,由于某些原因,它为什么不返回任何结果,我知道它在这里不起作用
文件中有数组。这就是为什么它返回null的原因。

1 个答案:

答案 0 :(得分:0)

您是否尝试过将查询放在聚合之上?