Mongodb $lookup with nested document

六月ゝ 毕业季﹏ 提交于 2019-12-11 09:07:20

问题


I am trying to create a join using mongo's lookup. I have these three collections.

orderTracking

{   
    _id: ObejctId("59fb7815b3b8429f4750b0df"),
    itemName : "Hamam Soap",
    TrackLocation: [{locationId: 1, at:"2017-10-11"},
            {locationId: 2,at:"2017-10-13"}],
    userId : 12,
    price: 20
} 

locationType

{
    _id: ObejctId("59b2111345cb72345a35fefd"),
    locationId : 1
    productTypeName: "Warehouse"
},{
    _id: ObejctId("59af8ce445cb72345a35feea"),
    locationId : 2
    productTypeName: "On Transit"
}

User

{
    _id: ObejctId("59a504eb6171b554c02292a9"),
            "user ID":12,
    "userName" : "Shahabaz Shafi",
    "dateOfBirth" : "1992-01-01",
    "addres": {
        "country" : "India",
        "state" : "Karnataka",
        "city" :  "Bengaluru"
    }

}

and trying to flatten this to this kind of output.

{
"userName" : "Shahabaz Shafi",
"userId":12,
"dateOfBirth" : "1992-01-01",
"country" : "India",
"state" : "Karnataka",
"city" :  "Bengaluru"

"locationType" : [ {productTypeName: "Warehouse",at:"2017-10-11"}, {productTypeName: "On Transit",at:"2017-10-13"}]
}

Edit: 15-11-2018 Updated output

Made some changes to the output columns

{
   "userName":"Shahabaz Shafi",
   "userId":12,
   "dateOfBirth":"1992-01-01",
   "country":"India",
   "state":"Karnataka",
   "city":"Bengaluru",
   "items":[
      {
         "itemName":"Hamam Soap",
         "userId":12,
         "price":20,
         "TrackLocation":[
            {
               "locationId":1,
               "at":"2017-10-11",
               "productTypeName":"Warehouse"
            },
            {
               "locationId":2,
               "at":"2017-10-13",
               "productTypeName":"On Transit"
            }
         ]
      }
   ]
}

How do I approach this ?

PS : I am also using compass


回答1:


You can use below aggregation with mongodb 3.6 and above

db.User.aggregate([
  { "$lookup": {
    "from": "orderTracking",
    "let": { "userId": "$userId" },
    "pipeline": [
      { "$match": { "$expr": { "$eq": ["$userId", "$$userId"] }}},
      { "$unwind": "$TrackLocation" },
      { "$lookup": {
        "from": "locationType",
        "let": { "location": "$TrackLocation.locationId" },
        "pipeline": [
          { "$match": { "$expr": { "$eq": ["$locationId", "$$location"] }}}
        ],
        "as": "locationType"
      }},
      { "$project": {
        "_id": 0,
        "productTypeName": { "$arrayElemAt": ["$locationType.productTypeName", 0] },
        "at": "$TrackLocation.at"
      }}
    ],
    "as": "locationType"
  }},
  { "$replaceRoot": { "newRoot": { "$mergeObjects": ["$addres", "$$ROOT"] }}},
  { "$project": { "addres": 0 }}
])

Output

[
  {
    "_id": ObjectId("59a504eb6171b554c02292a9"),
    "city": "Bengaluru",
    "country": "India",
    "dateOfBirth": "1992-01-01",
    "locationType": [
      {
        "at": "2017-10-11",
        "productTypeName": "Warehouse"
      },
      {
        "at": "2017-10-13",
        "productTypeName": "On Transit"
      }
    ],
    "state": "Karnataka",
    "userId": 12,
    "userName": "Shahabaz Shafi"
  }
]


来源:https://stackoverflow.com/questions/53306832/mongodb-lookup-with-nested-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!