
- 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
How to update array of subdocuments in MongoDB?
To update, use update() along with $set. Let us create a collection with documents −
>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") }
Display all documents from a collection with the help of find() method −
> db.demo134.find().pretty();
This will produce the following output −
{ "_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 } ] }
Following is the query to update array of subdocuments in MongoDB −
> db.demo134.update( ... { ... "EmployeeId":101, ... "EmployeeDetails.EmployeeName":"Chris" ... }, ... { ... $set: { ... "EmployeeDetails.$.EmployeeName" : "Robert" ... } ... } ... ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo134.find().pretty();
This will produce the following output −
{ "_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 } ] }
- Related Articles
- Query array of subdocuments in MongoDB
- How to keep appending subdocuments in MongoDB?
- Manipulating subdocuments in MongoDB
- MongoDB query to sort subdocuments
- How to update array with multiple conditions in MongoDB
- Update Array element in MongoDB?
- How do I access subdocuments in MongoDB queries?
- How to use arrays as filters by querying subdocuments in MongoDB?
- MongoDB query to update array object in index N?
- Update elements inside an array in MongoDB?
- MongoDB query to update array with another field?
- MongoDB query to update an array using FindAndUpdate()?
- Update field in exact element array in MongoDB?
- Update multiple elements in an array in MongoDB?
- Split a document by its subdocuments in MongoDB

Advertisements