/* Collection 1 : users */
{
"_id" : ObjectId("5c46eaf2e166363e18d6ef91"),
"userName" : "Shahabaz Shafi",
"dateOfBirth" : "1992-01-01",
"userId" : 12,
"userType" : 1,
"addres" : {
"country" : "India",
"state" : "Karnataka",
"city" : "Bengaluru"
}
}
/* Collection 2 : userTypes */
/* 1 */
{
"_id" : ObjectId("5c46eb46e166363e18d6ef92"),
"userTypeId" : 1,
"type" : "student"
}
/* 2 */
{
"_id" : ObjectId("5c46eb46e166363e18d6ef93"),
"userTypeId" : 2,
"type" : "staff"
}
/* Collection 3 : loans */
/* 1 */
{
"_id" : ObjectId("5c46eb8be166363e18d6ef94"),
"loanAmount" : 1000,
"loanType" : 1,
"userId" : 12
}
/* 2 */
{
"_id" : ObjectId("5c46eb8be166363e18d6ef95"),
"loanAmount" : 300,
"loanType" : 2,
"userId" : 12
}
/* Collection 4 : loanType */
/* 1 */
{
"_id" : ObjectId("5c46ebc1e166363e18d6ef96"),
"loanTypeId" : 1,
"type" : "Home Loan"
}
/* 2 */
{
"_id" : ObjectId("5c46ebc1e166363e18d6ef97"),
"loanTypeId" : 2,
"type" : "Bike Loan"
}
Solution:
db.users.aggregate([
{
// Join collections users and usertypes
$lookup : {
from: "userTypes",
localField: "userType",
foreignField: "userTypeId",
as: "userTypes"
}
},
{
// Merge keys of array of objects userTypes in existing document
$replaceRoot: { newRoot: { $mergeObjects: [ { $arrayElemAt: [ "$userTypes", 0 ] }, "$$ROOT" ] } }
},
{
// Remove keys userTypes and userTypeId
$project : {
"userTypes": 0,
"userTypeId" : 0
}
},
{
// Join current document obtained with loans collection
$lookup : {
from: "loans",
localField: "userId",
foreignField: "userId",
as: "loans"
}
},
{
// Break array obtained from loans into separate documents
$unwind : "$loans"
},
{
// Join current documents obtained with loanType collection
$lookup : {
from: "loanType",
localField: "loans.loanType",
foreignField: "loanTypeId",
as: "loanTypes"
}
},
{
// Break array obtained from loans into separate documents
$unwind: "$loanTypes"
},
{
// Display fields as per requirement and modify fields accordingly
$project : {
_id:"$_id",
"type" :1,
"userName" : 1,
"dateOfBirth" : 1,
"userId" : 1,
"userType" : 1,
"addres" : 1,
"loanAmount" : "$loans.loanAmount",
"loantype": "$loans.loanType",
"loanTypeName" : "$loanTypes.type",
"loanId" : "$loans._id"
}
},
{
// Merge documents based on condition given and push data in loans array as per output required
$group:{
_id : "$_id",
"type" : {"$first" :"$type" },
"userName" : {"$first" :"$userName" },
"dateOfBirth" : {"$first" :"$dateOfBirth" },
"userId" : {"$first" :"$userId" },
"userType" : {"$first" :"$userType" },
"addres" : {"$first" :"$addres" },
"loans":{
$push:{
"loanAmount" : "$loanAmount",
"loanType" : "$loantype",
"type" : "$loanTypeName",
"loanId" : "$loanId"
}
}
}
}
]);
Output:
/* 1 */
{
"_id" : ObjectId("5c46eaf2e166363e18d6ef91"),
"type" : "student",
"userName" : "Shahabaz Shafi",
"dateOfBirth" : "1992-01-01",
"userId" : 12,
"userType" : 1,
"addres" : {
"country" : "India",
"state" : "Karnataka",
"city" : "Bengaluru"
},
"loans" : [
{
"loanAmount" : 1000,
"loanType" : 1,
"type" : "Home Loan",
"loanId" : ObjectId("5c46eb8be166363e18d6ef94")
},
{
"loanAmount" : 300,
"loanType" : 2,
"type" : "Bike Loan",
"loanId" : ObjectId("5c46eb8be166363e18d6ef95")
}
]
}
@Anthony Winzlet @Shahabaz Please try this.