简体   繁体   中英

Mongodb aggregate lookup three collections

Learning MongoDB for the past two days and I am trying to aggregate three collections but unable to achieve it

Below are the three collection maintaining in the database

t_credentials

{
    "_id" : "619ca68b624c41e408348406",
    "title" : "Company ID"
}

t_groups

{
    "_id" : "61a253da88ca12a37218898d",
    "group_name" : "Gold"
}

t_user_credentials

{
    "_id" : "619ca88a624c41e408348424",
    "credential_id" : "619ca68b624c41e408348406",
    "group_id" : "61a253da88ca12a37218898d",
    "identifiers" : {
        "first_name" : "Lee",
        "middle_name" : "Min",
        "last_name" : "Ho"
    },
    "created_at" : "2021-12-01T17:20:49.000Z"
}

Here I am trying to achieve the output in the below format:

Expected Output

[{
    "_id" : "619ca88a624c41e408348424",
    "first_name" : ,
    "middle_name" : ,
    "last_name" : ,
    "credential" : {
        "_id:" : "619ca68b624c41e408348406",
        "title" : "Company ID"
    },
    "group" : {
        "_id" : "61a253da88ca12a37218898d",
        "group_name" : "Gold"
    },
    "created_at" : "2021-12-01T17:20:49.000Z"
}]

But, I am getting the fields only from t_user_credentials but not able to get like in the above format

Query

db.t_user_credentials.aggregate([
        {
            $lookup: {
               from: "t_credentials",
               localField: "_id",
               foreignField: "credential_id",
               as: "credentials"
            }
        },
        {
            $unwind: {
                path:'$credentials',
                preserveNullAndEmptyArrays: true
            }
        },
        {
            $lookup: {
                 from: "t_groups",
                 localField: "_id",
                 foreignField: "group_id",
                 as: "groups"
            }
        },
        {
            $unwind: {
                path: '$groups', 
                preserveNullAndEmptyArrays: true
            }
        },
        {
            $project: {
                last_name: "$identifiers.last_name",
                first_name: "$identifiers.first_name",
                middle_name: "$identifiers.middle_name",
                "credentials.title": 1,
                created_at: 1,
                group_id: 1
            }
        }
      ])

Can any one help me to solve this issue, it will be very helpful for me.

This query uses $replaceWith to merge the identifiers sub-document into the $$ROOT document. We also use $unset to remove fields we are no longer interested in. Before all of that we make sure to unwind our credential and group fields.

You can check out a live demo of this query here

Consider the following:

Database

db={
  "t_credentials": [
    {
      "_id": "619ca68b624c41e408348406",
      "title": "Company ID"
    }
  ],
  "t_groups": [
    {
      "_id": "61a253da88ca12a37218898d",
      "group_name": "Gold"
    }
  ],
  "t_user_credentials": [
    {
      "_id": "619ca88a624c41e408348424",
      "credential_id": "619ca68b624c41e408348406",
      "group_id": "61a253da88ca12a37218898d",
      "identifiers": {
        "first_name": "Lee",
        "middle_name": "Min",
        "last_name": "Ho"
      },
      "created_at": "2021-12-01T17:20:49.000Z"
    }
  ]
}

Query

db.t_user_credentials.aggregate([
  {
    "$lookup": {
      "from": "t_credentials",
      "localField": "credential_id",
      "foreignField": "_id",
      "as": "credential"
    }
  },
  {
    "$lookup": {
      "from": "t_groups",
      "localField": "group_id",
      "foreignField": "_id",
      "as": "group"
    }
  },
  {
    $unwind: "$group",
    
  },
  {
    $unwind: "$credential"
  },
  {
    $replaceWith: {
      $mergeObjects: [
        "$$ROOT",
        "$identifiers"
      ]
    }
  },
  {
    $unset: [
      "group_id",
      "credential_id",
      "identifiers"
    ]
  }
])

Result

[
  {
    "_id": "619ca88a624c41e408348424",
    "created_at": "2021-12-01T17:20:49.000Z",
    "credential": {
      "_id": "619ca68b624c41e408348406",
      "title": "Company ID"
    },
    "first_name": "Lee",
    "group": {
      "_id": "61a253da88ca12a37218898d",
      "group_name": "Gold"
    },
    "last_name": "Ho",
    "middle_name": "Min"
  }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM