अद्यतन करने के लिए, $set के साथ अद्यतन() का उपयोग करें। आइए दस्तावेजों के साथ एक संग्रह बनाएं -
>db.demo134.insertOne({"EmployeeId":101,"EmployeeDetails":[{"EmployeeName":"Chris","EmployeeAge":27},{"EmployeeName":"Bob","EmployeeAge":28}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e319b2f68e7f832db1a7f7c") } >db.demo134.insertOne({"EmployeeId":102,"EmployeeDetails":[{"EmployeeName":"David","EmployeeAge":24},{"EmployeeName":"Carol","EmployeeAge":29}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e319b4468e7f832db1a7f7d") }
संग्रह से सभी दस्तावेज़ों को खोजने () विधि की सहायता से प्रदर्शित करें -
> db.demo134.find().pretty();
यह निम्नलिखित आउटपुट उत्पन्न करेगा -
{ "_id" : ObjectId("5e319b2f68e7f832db1a7f7c"), "EmployeeId" : 101, "EmployeeDetails" : [ { "EmployeeName" : "Chris", "EmployeeAge" : 27 }, { "EmployeeName" : "Bob", "EmployeeAge" : 28 } ] } { "_id" : ObjectId("5e319b4468e7f832db1a7f7d"), "EmployeeId" : 102, "EmployeeDetails" : [ { "EmployeeName" : "David", "EmployeeAge" : 24 }, { "EmployeeName" : "Carol", "EmployeeAge" : 29 } ] }
MongoDB में उप-दस्तावेज़ों की सरणी को अद्यतन करने के लिए क्वेरी निम्नलिखित है -
> db.demo134.update( ... { ... "EmployeeId":101, ... "EmployeeDetails.EmployeeName":"Chris" ... }, ... { ... $set: { ... "EmployeeDetails.$.EmployeeName" : "Robert" ... } ... } ... ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
संग्रह से सभी दस्तावेज़ों को खोजने () विधि की सहायता से प्रदर्शित करें -
> db.demo134.find().pretty();
यह निम्नलिखित आउटपुट उत्पन्न करेगा -
{ "_id" : ObjectId("5e319b2f68e7f832db1a7f7c"), "EmployeeId" : 101, "EmployeeDetails" : [ { "EmployeeName" : "Robert", "EmployeeAge" : 27 }, { "EmployeeName" : "Bob", "EmployeeAge" : 28 } ] } { "_id" : ObjectId("5e319b4468e7f832db1a7f7d"), "EmployeeId" : 102, "EmployeeDetails" : [ { "EmployeeName" : "David", "EmployeeAge" : 24 }, { "EmployeeName" : "Carol", "EmployeeAge" : 29 } ] }