
- 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 multiple elements in an array in MongoDB?
To update multiple elements, use $[]. The $[] is an all positional operator indicating that the update operator should modify all elements in the specified array field.
Let us first create a collection with documents −
> db.demo385.insertOne({"ServerLogs": [ ... { ... "status":"InActive" ... }, ... { ... "status":"InActive" ... }, ... { ... "status":"InActive" ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e5b6a7522064be7ab44e7f5") }
Display all documents from a collection with the help of find() method −
> db.demo385.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e5b6a7522064be7ab44e7f5"), "ServerLogs" : [ { "status" : "InActive" }, { "status" : "InActive" }, { "status" : "InActive" } ] }
Following is the query to update multiple elements in an array in MongoDB −
> db.demo385.update( ... { "_id" : ObjectId("5e5b6a7522064be7ab44e7f5") }, ... { "$set": { "ServerLogs.$[].status": "Active" }} ... ) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo385.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e5b6a7522064be7ab44e7f5"), "ServerLogs" : [ { "status" : "Active" }, { "status" : "Active" }, { "status" : "Active" } ] }
- Related Articles
- Update elements inside an array in MongoDB?
- Perform multiple updates with bulk operations and update elements in an array in MongoDB
- How to update array with multiple conditions in MongoDB
- How to update multiple documents in MongoDB?
- Update Array element in MongoDB?
- Pull multiple objects from an array in MongoDB?
- Make an array of multiple arrays in MongoDB?
- Update an array of strings nested within an array of objects in MongoDB
- Update multiple rows in a single MongoDB query?
- Query to retrieve multiple items in an array in MongoDB?
- MongoDB query to update an array using FindAndUpdate()?
- Update values in multiple documents with multi parameter in MongoDB?
- Delete all elements in an array field in MongoDB?
- Update an array element matching a condition using $push in MongoDB
- Convert a field to an array using update operation in MongoDB

Advertisements