
- 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
MongoDB query to update array object in index N?
Use update() in MongoDB to update array object. The usage of dot notation is also required. Let us create a collection with documents −
> db.demo489.insertOne( ... { ... ... ... details : [{ ... id : 101, ... "Info1" : { ... "StudentName" : "Chris" ... }, ... "Info2" : { ... "TeacherName" : "David" ... } ... }, ... { ... id : 102, ... "Info1" : { ... "StudentName" : "Carol" ... }, ... "Info2" : { ... "TeacherName" : "Mike" ... } ... } ... ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e8356e0b0f3fa88e22790ba") }
Display all documents from a collection with the help of find() method −
> db.demo489.find();
This will produce the following output −
{ "_id" : ObjectId("5e8356e0b0f3fa88e22790ba"), "details" : [ { "id" : 101, "Info1" : { "StudentName" : "Chris" }, "Info2" : { "TeacherName" : "David" } }, { "id" : 102, "Info1" : { "StudentName" : "Carol" }, "Info2" : { "TeacherName" : "Mike" } } ] }
Following is the query to update array object −
> db.demo489.update({"details.id":102}, ... {$set: {"details.$.Info1.StudentName":"Robert", ... "details.$.Info2.TeacherName":"John", ... "details.$.CountryName" : "US" ... ... } ... }) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo489.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e8356e0b0f3fa88e22790ba"), "details" : [ { "id" : 101, "Info1" : { "StudentName" : "Chris" }, "Info2" : { "TeacherName" : "David" } }, { "id" : 102, "Info1" : { "StudentName" : "Robert" }, "Info2" : { "TeacherName" : "John" }, "CountryName" : "US" } ] }
- Related Articles
- Update object at specific Array Index in MongoDB?
- Update array in MongoDB document by variable index?
- MongoDB query to update array with another field?
- MongoDB query to update an array using FindAndUpdate()?
- Query Array for 'true' value at index n in MongoDB?
- MongoDB query to update tag
- MongoDB query to access an object in an array
- How to update MongoDB Object?
- MongoDB query to update selected fields
- MongoDB query to update nested document
- MongoDB query for Partial Object in an array
- Update object in array with a specific key in MongoDB
- MongoDB query to remove empty objects in an object-array?
- MongoDB query to update an array element matching a condition using $push?
- MongoDB query to update only certain fields?

Advertisements