MongoDB में नेस्टेड ऑब्जेक्ट पुनर्प्राप्त करने के लिए $ ऑपरेटर का उपयोग करें। पहले दस्तावेजों के साथ एक संग्रह बनाते हैं:
> db.queryNestedObject.insertOne({
"StudentName" : "James",
"StudentSubjectScore" : [
{"StudentMongoDBScore":98},
{"StudentCScore":92},
{"StudentJavaScore":91}
]
});
{ "acknowledged" : true, "insertedId" : ObjectId("5ccf49a9dceb9a92e6aa1962") }
संग्रह से सभी दस्तावेजों को find() विधि की सहायता से प्रदर्शित करने के लिए क्वेरी:
> db.queryNestedObject.find().pretty();
यह निम्नलिखित आउटपुट उत्पन्न करेगा:
{ "_id" : ObjectId("5ccf49a9dceb9a92e6aa1962"), "StudentName" : "James", "StudentSubjectScore" : [ { "StudentMongoDBScore" : 98 }, { "StudentCScore" : 92 }, { "StudentJavaScore" : 91 } ] }
नेस्टेड ऑब्जेक्ट पुनर्प्राप्त करने के लिए क्वेरी:
> db.queryNestedObject.find({'StudentSubjectScore.StudentJavaScore' : 91},{'StudentSubjectScore.$': 1 , _id: 0});
यह निम्नलिखित आउटपुट उत्पन्न करेगा:
{ "StudentSubjectScore" : [ { "StudentJavaScore" : 91 } ] }