निश्चित तत्व ढूंढने के लिए MongoDB में $project का उपयोग करें। आइए एक कलेक्शन बनाते हैं:
> db.demo744.insertOne( { studentInformation: [ { studentName:"Robert", grade:"A" }, { studentName:"Bob", grade:"C" }, { studentName:"John", grade:"B" }, { studentName:"Sam", grade:"A" } ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5ead928a57bb72a10bcf0684") }
find() से सभी डॉक्यूमेंट्स देखें:
> db.demo744.find();
आउटपुट:
{ "_id" : ObjectId("5ead928a57bb72a10bcf0684"), "studentInformation" : [ { "studentName" : "Robert", "grade" : "A" }, { "studentName" : "Bob", "grade" : "C" }, { "studentName" : "John", "grade" : "B" }, { "studentName" : "Sam", "grade" : "A" } ] }
एम्बेडेड डॉक्यूमेंट में निश्चित तत्व ढूंढने के लिए क्वेरी (ग्रेड "A" वाले छात्र):
> db.demo744.aggregate( { $unwind: '$studentInformation' }, { $match: {'studentInformation.grade':"A"}}, { $project: {"studentInformation.studentName": 1}} )
परिणाम:
{ "_id" : ObjectId("5ead928a57bb72a10bcf0684"), "studentInformation" : { "studentName" : "Robert" } }
{ "_id" : ObjectId("5ead928a57bb72a10bcf0684"), "studentInformation" : { "studentName" : "Sam" } }