चाइल्ड ऑब्जेक्ट्स को अपडेट करने के लिए, $set ऑपरेटर का उपयोग करें। आइए पहले दस्तावेज़ के साथ एक संग्रह बनाएं -
>db.updateChildObjectsDemo.insertOne({"StudentName":"Chris","StudentOtherDetails":{"StudentSubject":"MongoDB","StudentCountryName":"AUS"}});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ce964e078f00858fb12e91f")
} खोज () विधि की मदद से संग्रह से सभी दस्तावेजों को प्रदर्शित करने के लिए क्वेरी निम्नलिखित है -
> db.updateChildObjectsDemo.find().pretty();
यह निम्नलिखित आउटपुट देगा -
{
"_id" : ObjectId("5ce964e078f00858fb12e91f"),
"StudentName" : "Chris",
"StudentOtherDetails" : {
"StudentSubject" : "MongoDB",
"StudentCountryName" : "AUS"
}
} MongoDB में चाइल्ड ऑब्जेक्ट्स को अपडेट करने के लिए क्वेरी निम्नलिखित है -
> db.updateChildObjectsDemo.update({"StudentName" : "Chris"},{$set:{"StudentOtherDetails.StudentCountryName":"UK"}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) आइए एक बार फिर से दस्तावेज़ की जाँच करें -
> db.updateChildObjectsDemo.find().pretty();
यह निम्नलिखित आउटपुट देगा -
{
"_id" : ObjectId("5ce964e078f00858fb12e91f"),
"StudentName" : "Chris",
"StudentOtherDetails" : {
"StudentSubject" : "MongoDB",
"StudentCountryName" : "UK"
}
}