从mongodb中的数组中删除$ unwind操作

时间:2018-08-06 11:43:52

标签: node.js mongodb express aggregation-framework mlab

有多个集合之一。

  1. 用户
  2. 地点

存储在数据库中的文档,如下所示。

{
"_id" : "1eca51f0-538d-11e8-89fc-d125355b590d",
"organization" : [
    {
        "associationIds" : {
            "dObjectIds" : [
                {
                    "dObjectData" : {
                        "objectId" : "fd047ddf-61c2-4df2-8241-62af0399c8c1",
                        "objectName" : "shift",
                        "dObjectId" : "49492544-e4e3-491b-b967-a6f62330da3f"
                    }
                }
            ]
        },
        "organizationId" : "1c3f6150-538d-11e8-89fc-d125355b590d",
        "privelege" : "Admin",
        "organizationName" : "ABC"
    },
    {
        "associationIds" : {
            "dObjectIds" : [
                {
                    "dObjectData" : {
                        "objectId" : "ad047ddf-61c2-4df2-8241-62af0399c5c1",
                        "objectName" : "shift",
                        "dObjectId" : "38492544-e4e3-491b-b967-a6f62330da3f"
                    }
                },
                {
                    "dObjectData" : {
                        "objectId" : "fd047ddf-61c2-4df2-8241-62af0399c8c2",
                        "objectName" : "department",
                        "dObjectId" : "49492544-e4e3-491b-b967-a6f62330da1f"
                    }
                }
            ]
        },
        "organizationId" : "1c3f6150-538d-11e8-89fc-d125355b591d",
        "privelege" : "Manager",
        "organizationName" : "XYZ"
    }
]}

我正在使用这些查询。

 db.users.aggregate([
     {$match: {'_id': '1eca51f0-538d-11e8-89fc-d125355b590d'}},
     {$unwind :'$organization'},
     {$match:{'organization.organizationId':'1c3f6150-538d-11e8-89fc-d125355b591d'}},
     {$unwind:'$organization.associationIds.dObjectIds'},
      {'$lookup': {
          'from': 'places',
           localField:'organization.associationIds.dObjectIds.dObjectData.dObjectId',
           foreignField: 'dObjectData.dObjectId',
           as: 'associationData'
   }
 },
 {'$project': {
       'associationData': 1
   }
}
])

我在一次查询中使用了两次 $ unwind ,而该查询更昂贵。是否有任何方法或操作可以使用,而不是多次使用 $ unwind

注意 :我想这样的结果(associationData的结果从其他集合中获取了线索,所以我使用了 $ lookup

 {
_id" : "c6114ee0-c82f-11e6-9b1e-6140212f8693",
"associationData" : [
    {
        "_id" : "f09f5d70-ad7e-11e7-87bd-cd873956707e",
        "dObjectData" : {
            "departmentName" : "CEO & Directors",
            "dObjectId" : "ab034106-ad7d-11e7-abc4-cec278b6b50a",
            "objectName" : "Departments",
            "objectId" : "63735ee2-ad79-11e7-abc4-cec278b6b50a",
            "updatedOn" : ISODate("2017-10-10T11:20:37.255+05:30"),
            "addedOn" : ISODate("2017-10-10T11:20:37.255+05:30")
        },
        "associationIds" : {
            "organizationId" : "af39bc69-1938-4149-b9f7-f101fd9baf73",
            "placeIds" : [ ],
            "assignmentIds" : [ ],
            "beaconIds" : [ ],
            "userIds" : [ ]
        }
}]}

我会非常感激。

0 个答案:

没有答案