如何编写mongo查询来组合来自两个文档的数据 [英] How to write mongo query to combine data from two document

查看:122
本文介绍了如何编写mongo查询来组合来自两个文档的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文档,一个是userRole(Role Master),另一个是user(与用户关联的角色),用户可以有多个角色,并且在用户级别和角色级别都有一些权限,我想获取不同的权限来自用户和userRole的特定用户以及关联此用户的多个角色。



userRole:{
_id :ObjectId(58902f6b5d474004b4034026),
roleName:填充程序,
权限:{
ActionPermissions:{
发生率管理:{
故障单:{
添加故障单:{}
}
}
}
},
associatePermission:{
ActionPermissions:{
发生率管理:{
故障单:{
添加票证:{},
批量关闭:true
},
账龄报告:{
过滤报告:真
}
},
预防性维护:{
编辑门票:{
严重性:真实
}
}
},
DataPermissions:{
发生率管理:{
故障单: {
添加票证:{}
}
}
}
}
}





我尝试过:



user:{
_ id:ObjectId(58a29743f7d4180a4cae827e),
firstName:dileep,
roles:[
{
roleName:{
_id:5890336b5d474004b403402c,
roleName:首席执行官
},
effectiveFrom:2017-02-16T18:30:00.000Z,
effectiveTo:2017-02-28T18:30:00.000Z,
id:{}
},
{
effectiveFrom: -16T18:30:00.000Z,
effectiveTo:ISODate(2017-02-15T18:30:00.000Z),
roleName:{\_id\ :\5890335d5d474004b4034029 \,\roleName \:\Engineer \}
},
{
roleName:{
_id:5890336b5d474004b403402c,
roleName:首席执行官
},
effectiveFrom:2017-02-16T18:30:00.000Z,
effectiveTo:ISODate(2017-02-28T18:30:00.000Z),
id:{}
},
{
roleName:{
_id:589033795d474004b403402f,
roleName:WebUser
},
id:{}
},
{
roleName:{
_id:58a69b9a93e8e20854282001,
roleName:surbhi,
roleDescription:surbhi
},
effectiveFrom:2017-02- 23T18:30:00.000Z,
id:5
},
{
roleName:{
_id:58aef573d90ffd185c96bb65,
roleName:ty,
roleDescription:
},
effectiveFrom:2017-02-23T18:30:00.000Z,
id:6
}
],
群组:[
{
groupName:{
_id: 5891d9e75d4740205401bd68,
groupName:电源供应商
},
effectiveFrom:2017-02-17T18:30:00.000Z,
id: 1,
effectiveTo:ISODate(2017-02-15T18:30:00.000Z)
},
{
groupName:{
_id:5891d9f15d4740205401bd6b,
groupName:Tower Co
},
effectiveFrom:2017-02-21T18: 30:00.000Z,
effectiveTo:ISODate(2017-02-28T18:30:00.000Z),
id:2
},
{
groupName:{
_id:5891d9f15d4740205401bd6b,
groupName:Tower Co
},
effectiveFrom:2017- 02-21T18:30:00.000Z,
effectiveTo:ISODate(2017-02-21T18:30:00.000Z),
id:2
},
{
groupName:{
_id:58a6bfa3ea436926ac06a234,
groupName:asdasd,
groupDescription:asd
},
effectiveFrom:2017-02-27T18:30:00.000Z,
id:4
},
{
groupName:{
_id:58a6958e93e8e20854282000,
groupName:anas,
groupDescription:此组用于测试目的。
},
effectiveFrom:2017-02-20T18:30:00.000Z,
id:5,
effectiveTo:ISODate(2017- 02-21T18:30:00.000Z)
},
{
groupName:{
_id:58a69dbb93e8e20854282002,
groupName: GP,
groupDescription:gp用于测试
},
effectiveFrom:2017-02-23T18:30:00.000Z,
effectiveTo:2017-03-01T18:30:00.000Z,
id:6
},
{
effectiveFrom:ISODate(2017-02 -26T18:30:00.000Z),
groupName:{
groupName:Tower CoMPANY,
_id:5891d9f15d4740205401bd6b
},
effectiveTo:null,
id:7
}
],
profilePic:UserProfilePic-1487050584447.jpg,
罗ginId:dileep123,
password:null,
confirmPassword:null,
imeiNumber1:222222222222222222,
middleName:kumara ,
lastName:saxenaaaaaa,
primaryEmail:ok@gmail.com,
primaryPhoneNumber:1111111111,
associatePermission:{
ActionPermissions:{
发生率管理:{},
用户管理:{
用户:{
查看:是的,
添加:true
}
}
},
DataPermissions:{}
}
}

解决方案

你需要在这里使用聚合框架。你可以检查这里



因此您的查询将如下所示

首先您将用户与用户ID匹配并将数据传递给n ext运算符。

下一个运营商将使用userrole文档加入它,你可以让用户在列表中找到他们的角色。



 db.users.aggregate([{


match:{ userId youruserid}},{


lookup:{from: userRoles,localField: userId,foreignField: userId,as: userWithRoleList}},{

i have two document one is userRole(Role Master) and another is user(Role Asssociated with user),user can have multiple role and there is some permissions applied on user level and role level both , i want to fetch distinct permission from user and userRole for particular user and on associate multiple role on this user.

 "userRole":{
    "_id" : ObjectId("58902f6b5d474004b4034026"),
    "roleName" : "Filler",
    "permission" : {
        "ActionPermissions" : {
            "Incidence Management" : {
                "Trouble Ticketing" : {
                    "Add Ticket" : {}
                }
            }
        }
    },
    "associatePermission" : {
        "ActionPermissions" : {
            "Incidence Management" : {
                "Trouble Ticketing" : {
                    "Add Ticket" : {},
                    "Bulk Closure" : true
                },
                "Ageing Report" : {
                    "Filter Report" : true
                }
            },
            "Preventive Maintenance" : {
                "Edit Ticket" : {
                    "Severity" : true
                }
            }
        },
        "DataPermissions" : {
            "Incidence Management" : {
                "Trouble Ticketing" : {
                    "Add Ticket" : {}
                }
            }
        }
    }
}



What I have tried:

"user": {
    "_id" : ObjectId("58a29743f7d4180a4cae827e"),
    "firstName" : "dileep",
    "roles" : [ 
        {
            "roleName" : {
                "_id" : "5890336b5d474004b403402c",
                "roleName" : "Ceo"
            },
            "effectiveFrom" : "2017-02-16T18:30:00.000Z",
            "effectiveTo" : "2017-02-28T18:30:00.000Z",
            "id" : {}
        }, 
        {
            "effectiveFrom" : "2017-02-16T18:30:00.000Z",
            "effectiveTo" : ISODate("2017-02-15T18:30:00.000Z"),
            "roleName" : "{\"_id\":\"5890335d5d474004b4034029\",\"roleName\":\"Engineer\"}"
        }, 
        {
            "roleName" : {
                "_id" : "5890336b5d474004b403402c",
                "roleName" : "Ceo"
            },
            "effectiveFrom" : "2017-02-16T18:30:00.000Z",
            "effectiveTo" : ISODate("2017-02-28T18:30:00.000Z"),
            "id" : {}
        }, 
        {
            "roleName" : {
                "_id" : "589033795d474004b403402f",
                "roleName" : "WebUser"
            },
            "id" : {}
        }, 
        {
            "roleName" : {
                "_id" : "58a69b9a93e8e20854282001",
                "roleName" : "surbhi",
                "roleDescription" : "surbhi"
            },
            "effectiveFrom" : "2017-02-23T18:30:00.000Z",
            "id" : 5
        }, 
        {
            "roleName" : {
                "_id" : "58aef573d90ffd185c96bb65",
                "roleName" : "ty",
                "roleDescription" : ""
            },
            "effectiveFrom" : "2017-02-23T18:30:00.000Z",
            "id" : 6
        }
    ],
    "groups" : [ 
        {
            "groupName" : {
                "_id" : "5891d9e75d4740205401bd68",
                "groupName" : "Power Vendor"
            },
            "effectiveFrom" : "2017-02-17T18:30:00.000Z",
            "id" : 1,
            "effectiveTo" : ISODate("2017-02-15T18:30:00.000Z")
        }, 
        {
            "groupName" : {
                "_id" : "5891d9f15d4740205401bd6b",
                "groupName" : "Tower Co"
            },
            "effectiveFrom" : "2017-02-21T18:30:00.000Z",
            "effectiveTo" : ISODate("2017-02-28T18:30:00.000Z"),
            "id" : 2
        }, 
        {
            "groupName" : {
                "_id" : "5891d9f15d4740205401bd6b",
                "groupName" : "Tower Co"
            },
            "effectiveFrom" : "2017-02-21T18:30:00.000Z",
            "effectiveTo" : ISODate("2017-02-21T18:30:00.000Z"),
            "id" : 2
        }, 
        {
            "groupName" : {
                "_id" : "58a6bfa3ea436926ac06a234",
                "groupName" : "asdasd",
                "groupDescription" : "asd"
            },
            "effectiveFrom" : "2017-02-27T18:30:00.000Z",
            "id" : 4
        }, 
        {
            "groupName" : {
                "_id" : "58a6958e93e8e20854282000",
                "groupName" : "anas",
                "groupDescription" : "this group is made for testing purpose."
            },
            "effectiveFrom" : "2017-02-20T18:30:00.000Z",
            "id" : 5,
            "effectiveTo" : ISODate("2017-02-21T18:30:00.000Z")
        }, 
        {
            "groupName" : {
                "_id" : "58a69dbb93e8e20854282002",
                "groupName" : "GP",
                "groupDescription" : "gp is for test"
            },
            "effectiveFrom" : "2017-02-23T18:30:00.000Z",
            "effectiveTo" : "2017-03-01T18:30:00.000Z",
            "id" : 6
        }, 
        {
            "effectiveFrom" : ISODate("2017-02-26T18:30:00.000Z"),
            "groupName" : {
                "groupName" : "Tower CoMPANY",
                "_id" : "5891d9f15d4740205401bd6b"
            },
            "effectiveTo" : null,
            "id" : 7
        }
    ],
    "profilePic" : "UserProfilePic-1487050584447.jpg",
    "loginId" : "dileep123",
    "password" : null,
    "confirmPassword" : null,
    "imeiNumber1" : "222222222222222222",
    "middleName" : "kumara",
    "lastName" : "saxenaaaaaa",
    "primaryEmail" : "ok@gmail.com",
    "primaryPhoneNumber" : "1111111111",
    "associatePermission" : {
        "ActionPermissions" : {
            "Incidence Management" : {},
            "User Management" : {
                "Users" : {
                    "View" : true,
                    "Add" : true
                }
            }
        },
        "DataPermissions" : {}
    }
}

解决方案

Hi, you need to use aggregation framework here.you can check here

so your query will look like this
first you match the user with userid and pass the data to next operator.
next operator will join it with userrole document and you can get the user with their role in list.

db.users.aggregate([{


match:{"userId":"youruserid"}},{


lookup:{from:"userRoles",localField:"userId",foreignField:"userId",as:"userWithRoleList"}},{


这篇关于如何编写mongo查询来组合来自两个文档的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆