MongoDb $lookup query with multiple fields from objects array

蹲街弑〆低调 提交于 2020-01-06 20:14:51

问题


This question has previously been marked as a duplicate of this question I can with certainty confirm that it is not.

This is not a duplicate of the linked question because the elements in question are not an array but embedded in individual objects of an array as fields. I am fully aware of how the query in the linked question should work, however that scenario is different from mine.

I have a question regarding the $lookup query of MongoDb. My data structure looks as follows:

My "Event" collection contains this single document:

{
   "_id": ObjectId("mongodbobjectid..."),
   "name": "Some Event",
   "attendees": [
        {
           "type": 1,
           "status": 2,
           "contact": ObjectId("mongodbobjectidHEX1")
        },
        {
           "type": 7,
           "status": 4,
           "contact": ObjectId("mongodbobjectidHEX2")
        }
    ]
}

My "Contact" collection contains these documents:

{
    "_id": ObjectId("mongodbobjectidHEX1"),
    "name": "John Doe",
    "age": 35
},
{
    "_id": ObjectId("mongodbobjectidHEX2"),
    "name": "Peter Pan",
    "age": 60
}

What I want to do is perform an aggregate query with the $lookup operator on the "Event" collection and get the following result with full "contact" data:

{
   "_id": ObjectId("mongodbobjectid..."),
   "name": "Some Event",
   "attendees": [
        {
           "type": 1,
           "status": 2,
           "contact": {
              "_id": ObjectId("mongodbobjectidHEX1"),
              "name": "John Doe",
              "age": 35
           }
        },
        {
           "type": 7,
           "status": 4,
           "contact": {
              "_id": ObjectId("mongodbobjectidHEX2"),
              "name": "Peter Pan",
              "age": 60
           }
        }
    ]
}

I have done the same with single elements of "Contact" referenced in another document but never when embedded in an array. I am unsure of which pipeline arguments to pass to get the above shown result?

I also want to add a $match query to the pipeline to filter the data, but that is not really part of my question.


回答1:


Try this one

 db.getCollection('Event').aggregate([{ "$unwind": "$attendees" },
        { "$lookup" : { "from" : "Contact", "localField" : "attendees.contact", "foreignField": "_id", "as" : "contactlist" } },
        { "$unwind": "$contactlist" },
         { "$project" :{
                    "attendees.type" : 1,
                    "attendees.status" : 1,
                    "attendees.contact" : "$contactlist",
                      "name": 1, "_id": 1
                       }
        },
        {
            "$group" : {
                _id : "$_id" ,
                "name" : { $first : "$name" }, 
                "attendees" : { $push : "$attendees" }
            }
        }
        ])


来源:https://stackoverflow.com/questions/40609209/mongodb-lookup-query-with-multiple-fields-from-objects-array

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