Update के लिए simply update() का उपयोग करें। Sub collection तक पहुँचने और update करने के लिए $push operator के साथ dot notation का उपयोग करें।
Collection बनाना
> db.demo547.insertOne(
... {
... Name : "Chris",
... Test :
... {
... "FirstTest" :
... {
... Scores: [56,29,76]
... },
... "SecondTest" :
... {
... Scores: [98,91,78]
... }
... }
... }
... );
{ "acknowledged" : true, "insertedId" : ObjectId("5e8e2d579e5f92834d7f05dd") }
सभी Documents Display करना
> db.demo547.find();
Output:
{ "_id" : ObjectId("5e8e2d579e5f92834d7f05dd"), "Name" : "Chris", "Test" : { "FirstTest" : { "Scores" : [ 56, 29, 76 ] }, "SecondTest" : { "Scores" : [ 98, 91, 78 ] } } }
Concurrent Update with Subcollection Query
> db.demo547.update({"Name":"Chris"}, { $push:{ "Test.FirstTest.Scores" : 99}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Updated Documents Display करना
> db.demo547.find();
Output:
{ "_id" : ObjectId("5e8e2d579e5f92834d7f05dd"), "Name" : "Chris", "Test" : { "FirstTest" : { "Scores" : [ 56, 29, 76, 99 ] }, "SecondTest" : { "Scores" : [ 98, 91, 78 ] } } }
व्याख्या: Dot notation (Test.FirstTest.Scores) का उपयोग करके nested array तक पहुँचा गया और $push से नया score (99) add किया गया। यह atomic operation है।