
- 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 with multiple conditions in MongoDB
To update array with multiple conditions, use $push in MongoDB. Let us create a collection with documents −
> db.demo94.insertOne( ... { ... ... "Details" : [ ... { ... "Name" : "Chris", ... "Subject" : [] ... }, ... { ... "Name" : "David", ... "Subject" : [] ... }, ... { ... "Name" : "Bob", ... "Subject" : [] ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2d553bb8903cdd865577a9") }
Display all documents from a collection with the help of find() method −
> db.demo94.find();
This will produce the following output −
{ "_id" : ObjectId("5e2d553bb8903cdd865577a9"), "Details" : [ { "Name" : "Chris", "Subject" : [ ] }, { "Name" : "David", "Subject" : [ ] }, { "Name" : "Bob", "Subject" : [ ] } ] }
Following is the query to update array with multiple conditions in MongoDB −
> db.demo94.updateOne( ... { ... ... "Details": { "$elemMatch": { "Name": "David"}} ... }, ... { ... "$push": { "Details.$.Subject": { "Subject": "MongoDB" }} ... } ...); { "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
Display all documents from a collection with the help of find() method −
> db.demo94.find();
This will produce the following output −
{ "_id" : ObjectId("5e2d553bb8903cdd865577a9"), "Details" : [ { "Name" : "Chris", "Subject" : [ ] }, { "Name" : "David", "Subject" : [ { "Subject" : "MongoDB" } ] }, { "Name" : "Bob", "Subject" : [ ] } ] }
- Related Articles
- MongoDB query to $pull / $unset with multiple conditions?
- Update multiple elements in an array in MongoDB?
- How to update multiple documents in MongoDB?
- Insert array where element does not exist else update it (with multiple conditions)?
- Implement multiple conditions in MongoDB?
- MongoDB query to get documents with multiple conditions set in $or?
- Perform multiple updates with bulk operations and update elements in an array in MongoDB
- Update quantity in MongoDB based on two conditions?
- Update values in multiple documents with multi parameter in MongoDB?
- MongoDB query to update array with another field?
- How to update array of subdocuments in MongoDB?
- How to Update multiple documents in a MongoDB collection using Java?
- How to match multiple criteria inside an array with MongoDB?
- MongoDB multiple OR conditions on same key?
- How to calculate the median with multiple conditions in Excel?

Advertisements