MongoDB对嵌套文档进行$ lookup并根据需要准备结果

时间:2018-07-21 09:35:52

标签: mongodb mongodb-query

我有两个集合,一个是权限,另一个是角色。现在,我需要通过按权限对象ID连接从这两个集合中检索数据,我的查询工作正常,但无法按要求获取结果。集合详细信息如下。

权限:

{
"_id" : ObjectId("5b4998d4abb4369f8c1ad0d6"),
"roleName" : "Super top level",
"permissions" : [ 
    {
        "_id" : ObjectId("5b49982d34f95d80609ce99d"),
        "access" : [ 
            "view_graph", 
            "view_daily_trip", 
            "view_profit"
        ]
    }, 
    {
        "_id" : ObjectId("5b49982d34f95d80609ce99e"),
        "access" : [ 
            "view", 
            "add", 
            "edit", 
            "delete", 
            "special", 
            "disable"
        ]
    }, 
    {
        "_id" : ObjectId("5b49982d34f95d80609ce99f"),
        "access" : [ 
            "trip_view", 
            "trip_add", 
            "trip_edit", 
            "trip_delete", 
            "trip_special", 
            "trip_disable"
        ]
    }
]

}

角色:

{
"_id" : ObjectId("5b4998d4abb4369f8c1ad0d6"),
"roleName" : "Super top level",
"permissions" : [ 
    {
        "_id" : ObjectId("5b49982d34f95d80609ce99d"),
        "access" : [ 
            "view_graph",
            "view_daily_trip",
            "view_profit"
        ]
    }, 
    {
        "_id" : ObjectId("5b49982d34f95d80609ce99e"),
        "access" : [ 
            "view",
            "add",
            "edit",
            "delete"
        ]
    }, 
    {
        "_id" : ObjectId("5b49982d34f95d80609ce99f"),
        "access" : [ 
            "trip_view",
            "trip_add",
            "trip_edit"
        ]
    }
]

}

所需结果:

[
{
    "_id": "5b4998d4abb4369f8c1ad0d6",
    "roleName": "Mid level",
    "permissions": [
        {
            "_id": "5b49982d34f95d80609ce99d",
            "access": [
                "view_graph",
                "view_daily_trip",
                "view_profit"
            ],
            "permissionGroups": "Dashboard",
            "allAccess": [
                "view_graph",
                "view_daily_trip",
                "view_profit"
            ]
        },
        {
            "_id": "5b49982d34f95d80609ce99e",
            "access": [
                "view",
                "add",
                "edit",
                "delete"
            ],
            "permissionGroups": "User",
            "allAccess": [
                "view",
                "add",
                "edit",
                "delete",
                "special",
                "disable"
            ]
        },
        {
            "_id": "5b49982d34f95d80609ce99f",
            "access": [
                "trip_view",
                "trip_add",
                "trip_edit"
            ],
            "permissionGroups": "Trip",
            "access": [
                "trip_view",
                "trip_add",
                "trip_edit",
                "trip_delete",
                "trip_special",
                "trip_disable"
            ]
        }
    ]
}

]

我已经使用$lookup, $unwind, $project$group进行了很多尝试,但是没有得到任何结果。 您能帮我写出确切的查询吗?您的帮助将不胜感激。 谢谢:)

1 个答案:

答案 0 :(得分:0)

OOOffOff