नेस्टेड ऑब्जेक्ट में मान बढ़ाने के लिए, आप $inc ऑपरेटर का उपयोग कर सकते हैं। आइए पहले दस्तावेजों के साथ एक संग्रह बनाने के लिए निम्नलिखित क्वेरी को लागू करें
>db.incrementValueDemo.insertOne({"StudentName":"Larry","StudentCountryName":"US","StudentDetails":[{"StudentSubjectName":"Math","StudentMathMarks":79}]});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c986ca0330fd0aa0d2fe4a2")
} खोज () विधि की मदद से संग्रह से सभी दस्तावेजों को प्रदर्शित करने के लिए क्वेरी निम्नलिखित है
> db.incrementValueDemo.find().pretty();
यह निम्नलिखित आउटपुट उत्पन्न करेगा
{
"_id" : ObjectId("5c986ca0330fd0aa0d2fe4a2"),
"StudentName" : "Larry",
"StudentCountryName" : "US",
"StudentDetails" : [
{
"StudentSubjectName" : "Math",
"StudentMathMarks" : 79
}
]
} नेस्टेड ऑब्जेक्ट में मान बढ़ाने के लिए क्वेरी निम्नलिखित है। यहां अंक बढ़ेंगे
> db.incrementValueDemo.update( {"StudentDetails.StudentSubjectName":"Math"}, { $inc : {
"StudentDetails.$.StudentMathMarks" : 1 } });
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) यह जांचने के लिए क्वेरी निम्नलिखित है कि मान बढ़ा है या नहीं
> db.incrementValueDemo.find().pretty();
यह निम्नलिखित आउटपुट उत्पन्न करेगा
{
"_id" : ObjectId("5c986ca0330fd0aa0d2fe4a2"),
"StudentName" : "Larry",
"StudentCountryName" : "US",
"StudentDetails" : [
{
"StudentSubjectName" : "Math",
"StudentMathMarks" : 79
}
]
}