1

Colleges

/* 1 createdAt:5/9/2019, 7:00:04 PM*/
{
    "_id" : ObjectId("5cd42b5c65b41027845938ae"),
    "clgID" : "100",
    "name" : "Anna University"
},

/* 2 createdAt:5/9/2019, 7:00:04 PM*/
{
    "_id" : ObjectId("5cd42b5c65b41027845938ad"),
    "clgID" : "200",
    "name" : "National"
}

Subjects:

/* 1 createdAt:5/9/2019, 7:03:24 PM*/
{
    "_id" : ObjectId("5cd42c2465b41027845938b0"),
    "name" : "Hindi",
    "members" : {
        "student" : [
            "123"
        ]
    },
    "college" : {
        "collegeID" : "100"
    }
},

/* 2 createdAt:5/9/2019, 7:03:24 PM*/
{
    "_id" : ObjectId("5cd42c2465b41027845938af"),
    "name" : "English",
    "members" : {
        "student" : [
            "456",
            "789"
        ]
    },
    "college" : {
        "collegeID" : "100"
    }
}

Here i am having two collection and i want to join Colleges table is clgID and Subjects table iscollege.collegeID , then i want to take members.student values and push into single array based on college.collegeID.

My Expected Output

{
    "GroupDetails" : [ ],
    "clgName" : "National"
},
{
    "GroupDetails" : [
        "123",
        "456",
        "789"
    ],
    "clgName" : "Anna University"
}

My Code

db.Colleges.aggregate([
    { $match : { "clgID" : { $in :  ["100", "200"]  } } },
    { $lookup: { from: "Subjects", localField: "clgID", foreignField: "college.collegeID", as: "GroupDetails" } },
    //{ $unwind: "$GroupDetails" },
    { $project: { '_id' : false, 'clgName' : '$name', 'GroupDetails.members.student' : true } }
])

I am getting like this

/* 1 */
{
    "GroupDetails" : [ ],
    "clgName" : "National"
},

/* 2 */
{
    "GroupDetails" : [
        {
            "members" : {
                "student" : [
                    "456"
                ]
            }
        },
        {
            "members" : {
                "student" : [
                    "123"
                ]
            }
        }
    ],
    "clgName" : "Anna University"
}

1 Answer 1

2

You can use below aggregation with mongodb 3.6 and above

db.Colleges.aggregate([
  { "$match": { "clgID": { "$in": ["100", "200"] } } },
  { "$lookup": {
    "from": "Subjects",
    "let": { "clgId": "$clgID" },
    "pipeline": [
      { "$match": { "$expr": { "$eq": ["$$clgId", "$college.collegeID"] } } },
      { "$group": {
        "_id": "$college.collegeID",
        "groupDetails": { "$push": "$members.student" }
      }},
      { "$project": {
        "groupDetails": {
          "$reduce": {
            "input": "$groupDetails",
            "initialValue": [],
            "in": { "$concatArrays": ["$$this", "$$value"] }
          }
        }
      }}
    ],
    "as": "clg"
  }},
  { "$unwind": { "path": "$clg", "preserveNullAndEmptyArrays": true } },
  { "$project": {
    "clgName": "$name",
    "groupDetails": { "$ifNull": ["$clg.groupDetails", []] }
  }}
])

MongoPlayground

Or with the mongodb 3.4 and below

db.Colleges.aggregate([
  { "$match": { "clgID": { "$in": ["100", "200"] }}},
  { "$lookup": {
    "from": "Subjects",
    "localField": "clgID",
    "foreignField": "college.collegeID",
    "as": "clg"
  }},
  { "$unwind": { "path": "$clg", "preserveNullAndEmptyArrays": true }},
  { "$group": {
    "_id": { "clgId": "$clg.college.collegeID", "_id": "$_id" },
    "groupDetails": { "$push": "$clg.members.student" },
    "clgName": { "$first": "$name" }
  }},
  { "$project": {
    "_id": "$_id._id",
    "clgName": 1,
    "groupDetails": {
      "$reduce": {
        "input": "$groupDetails",
        "initialValue": [],
        "in": { "$concatArrays": ["$$this", "$$value"] }
      }
    }
  }}
])

MongoPlayground

Sign up to request clarification or add additional context in comments.

3 Comments

Kindly check this link
above link related to this question only, please check
Can you please check my question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.