
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
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.
- Related Articles
- Update Array element in MongoDB?
- Update _id field in MongoDB
- MongoDB query to update array with another field?
- Convert a field to an array using update operation in MongoDB
- MongoDB exact array matching
- Get index of given element in array field in MongoDB?
- How to update _id field in MongoDB?
- Update an array element matching a condition using $push in MongoDB
- Convert a field to an array using MongoDB update operation?
- Want to update inner field in a MongoDB
- Conditional update depending on field matched in MongoDB
- Aggregate a $slice to get an element in exact position from a nested array in MongoDB?
- Update MongoDB field using value of another field?
- Reverse array field in MongoDB?
- Update elements inside an array in MongoDB?
