MongoDB में स्थिति ऑपरेटर ($) से नेस्टेड सरणी में तत्व जोड़ें
आइए पहले दस्तावेज़ों के साथ एक संग्रह बनाते हैं:
>db.demo11.insertOne({"ListOfStudent":[{"StudentName":"Chris","ListOfScore":[76,67,54,89]}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e0f6e34d7df943a7cec4fa1") }
find().pretty() से सभी दस्तावेज़ प्रदर्शित करें:
> db.demo11.find().pretty();
आउटपुट:
{ "_id" : ObjectId("5e0f6e34d7df943a7cec4fa1"), "ListOfStudent" : [ { "StudentName" : "Chris", "ListOfScore" : [ 76, 67, 54, 89 ] } ] }
शर्त के साथ सरणी तत्व डालने का क्वेरी (StudentName="Chris" वाले दस्तावेज़ में ListOfScore में 98 जोड़ें):
> db.demo11.update( {"ListOfStudent.StudentName":"Chris"}, {$push:{"ListOfStudent.$.ListOfScore":98}} );
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
दस्तावेज़ फिर से जांचें:
> db.demo11.find().pretty();
आउटपुट:
{ "_id" : ObjectId("5e0f6e34d7df943a7cec4fa1"), "ListOfStudent" : [ { "StudentName" : "Chris", "ListOfScore" : [ 76, 67, 54, 89, 98 ] } ] }
व्याख्या: $ स्थिति ऑपरेटर उस सरणी तत्व की पहचान करता है जो क्वेरी शर्त से मेल खाता है। $push उस तत्व की नेस्टेड सरणी में मान जोड़ता है।