Update field in exact element array in MongoDB?



You can update the in exact element array in MongoDB with the help of below statement. The syntax is as follows:

{"yourArrayDocumentName.$.yourNestedArrayDocument.yourPosition":"yourValue"}});

To understand the above syntax, let us create a collection with some documents. The query to create a collection with document is as follows:

> db.updateExactField.insertOne({"ActorId":1,"ActorDetails":[{"ActorName":"Johnny Depp","MovieList": ["The Tourist","Public Enemy"]}, ... {"ActorName":"Chris Evans","MovieList":["Captain America","Avengers"]}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6d7f63f2db199c1278e7f1") }

Now you can display documents from a collection with the help of find() method. The query is as follows:

> db.updateExactField.find().pretty();

The following is the output:

{    "_id" : ObjectId("5c6d7f63f2db199c1278e7f1"),    "ActorId" : 1,    "ActorDetails" : [       {          "ActorName" : "Johnny Depp",          "MovieList" : [             "The Tourist",             "Public Enemy"          ]       },       {          "ActorName" : "Chris Evans",          "MovieList" : [             "Captain America",             "Avengers"          ]       }    ] }

Case 1: Here, update with exact array elements i.e. position 3rd, which is index 2 because array starts from 0. The query is as follows:

> db.updateExactField.update( ... {"ActorDetails.ActorName":"Chris Evans"}, ... {$set: ... {"ActorDetails.$.MovieList.2":"Avengers:Infinity War"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

Let us display the document from the collection with the help of find(). The query is as follows:

> db.updateExactField.find().pretty();

The following is the output:

{    "_id" : ObjectId("5c6d7f63f2db199c1278e7f1"),    "ActorId" : 1,    "ActorDetails" : [       {          "ActorName" : "Johnny Depp",          "MovieList" : [             "The Tourist",             "Public Enemy"          ]       },       {          "ActorName" : "Chris Evans",          "MovieList" : [             "Captain America",             "Avengers",             "Avengers:Infinity War"          ]       }    ] }

Look at the above output, the value “Avengers:Infinite War” is at position 3rd i.e. index 2.

Case 2: Let us now update with index 1 i.e. position 2nd. The query is as follows:

> db.updateExactField.update( {"ActorDetails.ActorName":"Chris Evans"}, {$set: {"ActorDetails.$.MovieList.1":"Gifted"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

Let us check the document from a collection with the help of find() method. The query is as follows:

> db.updateExactField.find().pretty();

The following is the output:

{    "_id" : ObjectId("5c6d7f63f2db199c1278e7f1"),    "ActorId" : 1,    "ActorDetails" : [       {          "ActorName" : "Johnny Depp",          "MovieList" : [             "The Tourist",             "Public Enemy"          ]       },       {          "ActorName" : "Chris Evans",          "MovieList" : [             "Captain America",             "Gifted",             "Avengers:Infinity War"          ]       }    ] }

Look at the above sample output, the value “Gifted” is at position 2 i.e. index 1.

Updated on: 2019-07-30T22:30:25+05:30

295 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements