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