2017-02-28 45 views
0

我有兩個文檔一個是userRole(角色大師),另一個是用戶(角色Asssociated與用戶),用戶可以有多個角色,並且有一些權限適用於用戶級別和角色級別兩者,我想從用戶和userRole獲取不同的權限,特定用戶和關聯此用戶上的多個角色.. ??mongo查詢合併來自兩個文檔的數據

"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" : {} 
 
       } 
 
      } 
 
     } 
 
    } 
 
}

"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" : "[email protected]", 
 
    "primaryPhoneNumber" : "1111111111", 
 
    "associatePermission" : { 
 
     "ActionPermissions" : { 
 
      "Incidence Management" : {}, 
 
      "User Management" : { 
 
       "Users" : { 
 
        "View" : true, 
 
        "Add" : true 
 
       } 
 
      } 
 
     }, 
 
     "DataPermissions" : {} 
 
    } 
 
}

回答

0

你需要$lookuphttps://docs.mongodb.com/master/reference/operator/aggregation/lookup/#pipe._S_lookup

這裏是一個good example引起共鳴如何去了解它

+0

@Antonio Narkevich,你能幫我嗎.. –

+0

我試過使用上面的共享鏈接,但無法找到想要的輸出,實際上我希望從RoleID join..e.g的集合基礎獲得不同的關聯權限。在用戶集合中,如果用戶具有2個權限並且用戶與3個角色關聯,則所有角色權限都應該與該文檔一起提供。 –

+0

從角色中選擇權限,其中roleID在(用戶ID爲123的用戶的SELECT角色ID)其中userID = 123;我想在mongoDB中寫上面的查詢 –