aggregate() के साथ $unwind का उपयोग करके गहरे सब-लिस्ट्स को कनकैट करें। कलेक्शन बनाएं:
> db.demo70.insertOne({
"first": [
{
"details": {
"second": [
{ "StudentDetails": { "Score": 10 } },
{ "StudentDetails": { "Score": 20 } },
{ "StudentDetails": { "Score": 30 } }
]
}
},
{
"details": {
"second": [
{ "StudentDetails": { "Score": 11 } },
{ "StudentDetails": { "Score": 18 } },
{ "StudentDetails": { "Score": 29 } }
]
}
}
]
});
{ "acknowledged": true, "insertedId": ObjectId("5e29ad4d0912fae76b13d76d") }
सभी डॉक्यूमेंट्स देखें:
> db.demo70.find().pretty();
आउटपुट:
गहरे सब-लिस्ट्स कनकैट करने की क्वेरी:
> db.demo70.aggregate([ { $unwind: "$first" }, { $unwind: "$first.details.second" }, { $sort: { "first.details.second.StudentDetails.Score": -1 } }, { $limit: 3 }, { $replaceRoot: { newRoot: "$first.details.second.StudentDetails" } }, { $sort: { "Score": 1 } } ]);परिणाम: