Mongodb brings together three collections

Studying MongoDB in the last two days, and I am trying to combine the three collections, but cannot achieve this.

Below are four collections in the database.

university

{
   "_id" : "5834ecf7432d92675bde9d82",
   "name": "NIFT"
}

college

{
   "_id" : "5834ecf7432d92675bde9d83",
   "name": "NIFT Hyderabad",
   "university_id":"5834ecf7432d92675bde9d82"
}

departments

{
   "_id" : "5834ecf7432d92675bde9d84",
   "department_name": "Fashion Technology",
   "college_id" : "5834ecf7432d92675bde9d83" 
},
{
   "_id" : "5834ecf7432d92675bde9d85",
   "department_name": "Merchandising",
   "college_id" : "5834ecf7432d92675bde9d83"
}

Sections

{
   "_id" : "5834ecf7432d92675bde9d86",
   "section_name": "A",
   "students" : "56",
   "department_id":"5834ecf7432d92675bde9d84"
},
{
   "_id" : "5834ecf7432d92675bde9d87",
   "section_name": "B",
   "students" : "60",
   "department_id":"5834ecf7432d92675bde9d84"
},
{
   "_id" : "5834ecf7432d92675bde9d86",
   "section_name": "A",
   "students" : "55",
   "department_id":"5834ecf7432d92675bde9d85"
},
{
   "_id" : "5834ecf7432d92675bde9d87",
   "section_name": "B",
   "students" : "44",
   "department_id":"5834ecf7432d92675bde9d85"
}

Here I am trying to get the result in the following format

Expected Result

[{
   "_id": "5834ecf7432d92675bde9d83",
   "name": "NIFT Hyderabad",
   "university_id": "5834ecf7432d92675bde9d82",
   "departments": [{
        "_id": "5834ecf7432d92675bde9d84",
        "department_name": "CSE",
        "college_id": "5834ecf7432d92675bde9d83",
        "sections": [{
            "_id": "5834ecf7432d92675bde9d86",
            "section_name": "A",
            "students": "56",
            "department_id": "5834ecf7432d92675bde9d84"
        }, {
            "_id": "5834ecf7432d92675bde9d87",
            "section_name": "B",
            "students": "60",
            "department_id": "5834ecf7432d92675bde9d84"
        }]
    },
    {
        "_id": "5834ecf7432d92675bde9d85",
        "department_name": "Mechanical",
        "college_id": "5834ecf7432d92675bde9d83",
        "sections": [{
                "_id": "5834ecf7432d92675bde9d86",
                "section_name": "A",
                "students": "55",
                "department_id": "5834ecf7432d92675bde9d85"
            },
            {
                "_id": "5834ecf7432d92675bde9d87",
                "section_name": "B",
                "students": "44",
                "department_id": "5834ecf7432d92675bde9d85"
            }
        ]
    }
  ]
}]

But, I get departments and sections in separate arrays for college, but I can’t get as in the above format

Query

db.college.aggregate([
            {"$match": { "university_id": "5834ecf7432d92675bde9d82" } },
            {"$lookup": {
            "localField": "_id",
            "from": "departments",
            "foreignField": "college_id",
            "as": "departments"
        }},
        {"$unwind":"$departments"},
        {$group : {_id : "$_id", departments : {$push : "$departments" }}},
        {"$lookup": {
        "localField": "departments._id",
        "from": "sections",
        "foreignField": "department_id",
        "as": "sections"}
        }
        ])

Can someone help me solve this problem, it will be very helpful for me.

+4
source share
1 answer

You can try to complete the aggregation request.

sections department, , $group, , .

db.college.aggregate([
  {
    "$match": {
      "university_id": "5834ecf7432d92675bde9d82"
    }
  },
  {
    "$lookup": {
      "localField": "_id",
      "from": "departments",
      "foreignField": "college_id",
      "as": "departments"
    }
  },
  {
   "$unwind": {
     "path": "$departments",
     "preserveNullAndEmptyArrays": true
    }
  },
  {
    "$lookup": {
      "localField": "departments._id",
      "from": "sections",
      "foreignField": "department_id",
      "as": "departments.sections"
    }
  },
  {
    "$group": {
      "_id": "$_id",
      "name": {
        "$first": "$name"
      },
      "university_id": {
        "$first": "$university_id"
      },
      "departments": {
        "$push": "$departments"
      }
    }
  }
])
+2

Source: https://habr.com/ru/post/1686338/


All Articles