MongoDB में एम्बेडेड दस्तावेजों को क्वेरी करने के लिए, aggregate() का उपयोग करें। आइए दस्तावेजों के साथ एक कलेक्शन बनाते हैं:
> db.demo705.insertOne(
{
_id:101,
"Information": [
{ "StudentName":"Chris", "StudentAge":21 },
{ "StudentName":"David", "StudentAge":23 },
{ "StudentName":"Bob", "StudentAge":20 }
]
}
); { "acknowledged" : true, "insertedId" : 101 }
find() मेथड की मदद से कलेक्शन के सभी दस्तावेज प्रदर्शित करें:
> db.demo705.find();
यह निम्न आउटपुट देगा:
{ "_id" : 101, "Information" : [ { "StudentName" : "Chris", "StudentAge" : 21 }, { "StudentName" : "David", "StudentAge" : 23 }, { "StudentName" : "Bob", "StudentAge" : 20 } ] }
MongoDB में एम्बेडेड दस्तावेजों को क्वेरी करने का तरीका (उम्र 21 या अधिक वाले छात्र):
> db.demo705.aggregate([
{ $unwind: "$Information" },
{ $match: {"Information.StudentAge": {$gte: 21}} },
{ $project: {Information: 1} }
])
यह निम्न आउटपुट देगा:
{ "_id" : 101, "Information" : { "StudentName" : "Chris", "StudentAge" : 21 } }
{ "_id" : 101, "Information" : { "StudentName" : "David", "StudentAge" : 23 } }