$inc और पोजिशनल ऑपरेटर $ से नेस्टेड ऐरे ऑब्जेक्ट अपडेट करें
MongoDB में ऐरे के अंदर नेस्टेड ऑब्जेक्ट के फील्ड को बढ़ाने (increment) के लिए $inc के साथ पोजिशनल $ ऑपरेटर का उपयोग करें।
उदाहरण डेटा
दस्तावेज़ देखें:
{ "_id" : ObjectId("5c986ca0330fd0aa0d2fe4a2"), "StudentName" : "Larry", "StudentCountryName" : "US", "StudentDetails" : [ {"StudentSubjectName" : "Math", "StudentMathMarks" : 79} ] }StudentSubjectName = "Math" वाले एलिमेंट के StudentMathMarks को 1 से बढ़ाएं
अपडेट के बाद:
{ "_id" : ObjectId("5c986ca0330fd0aa0d2fe4a2"), "StudentName" : "Larry", "StudentCountryName" : "US", "StudentDetails" : [ {"StudentSubjectName" : "Math", "StudentMathMarks" : 80} ] }व्याख्या:
{"StudentDetails.StudentSubjectName": "Math"}- क्वेरी उस दस्तावेज़ को ढूंढती है जहां ऐरे में यह मान हो$inc: {"StudentDetails.$.StudentMathMarks": 1}-$पोजिशनल ऑपरेटर क्वेरी से मिलान होने वाले ऐरे एलिमेंट की स्थिति को संदर्भित करता है
नोट: पोजिशनल $ ऑपरेटर केवल पहले मिलान वाले ऐरे एलिमेंट को अपडेट करता है।