
- 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 elements inside an array in MongoDB?
To update elements inside an array, use $set in MongoDB. Let us create a collection with documents −
> db.demo494.insertOne( ... { ... ... "CollegeDetails" : [ ... { ... "CollegeName" : "MIT", ... "Fees" : 80000 ... }, ... { ... "CollegeName" : "SU", ... "Fees" : 90000 ... } ... ] ... } ... ) { "acknowledged" : true, "insertedId" : ObjectId("5e84a5c1b0f3fa88e22790c9") }
Display all documents from a collection with the help of find() method −
> db.demo494.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e84a5c1b0f3fa88e22790c9"), "CollegeDetails" : [ { "CollegeName" : "MIT", "Fees" : 80000 }, { "CollegeName" : "SU", "Fees" : 90000 } ] }
Following is the query to update elements inside an array in MongoDB −
> db.demo494.update( ... ... { ... "CollegeDetails.CollegeName": "MIT" ... }, ... ... { ... $set: ... { ... "CollegeDetails.$.Fees" : 100000 ... } ... } ... ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo494.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e84a5c1b0f3fa88e22790c9"), "CollegeDetails" : [ { "CollegeName" : "MIT", "Fees" : 100000 }, { "CollegeName" : "SU", "Fees" : 90000 } ] }
- Related Articles
- Update multiple elements in an array in MongoDB?
- Perform multiple updates with bulk operations and update elements in an array in MongoDB
- Update Array element in MongoDB?
- Update an array of strings nested within an array of objects in MongoDB
- MongoDB query to update an array using FindAndUpdate()?
- How to increment inside the update() function in MongoDB?
- Match multiple criteria inside an array with MongoDB?
- Update an array element matching a condition using $push in MongoDB
- Convert a field to an array using update operation in MongoDB
- MongoDB query to find data from an array inside an object?
- How to get the matching document inside an array in MongoDB?
- Convert a field to an array using MongoDB update operation?
- MongoDB Aggregation to slice array inside array
- Delete all elements in an array field in MongoDB?
- How to match multiple criteria inside an array with MongoDB?

Advertisements