आप एकत्रीकरण क्वेरी के नीचे कोशिश कर सकते हैं।
नीचे दी गई क्वेरी 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"
}
}
}
])