
- 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
Multiple atomic updates using MongoDB?
For multiple atomic updates, use update() along with $set. Let us create a collection with documents −
> db.demo699.insertOne({Name:"Chris Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6e370551299a9f98c93a7") } > db.demo699.insertOne({Name:"David Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6e37a551299a9f98c93a8") } > db.demo699.insertOne({Name:"Chris Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6e381551299a9f98c93a9") } > db.demo699.insertOne({Name:"John Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6e38a551299a9f98c93aa") }
Display all documents from a collection with the help of find() method −
> db.demo699.find();
This will produce the following output −
{ "_id" : ObjectId("5ea6e370551299a9f98c93a7"), "Name" : "Chris Brown" } { "_id" : ObjectId("5ea6e37a551299a9f98c93a8"), "Name" : "David Miller" } { "_id" : ObjectId("5ea6e381551299a9f98c93a9"), "Name" : "Chris Brown" } { "_id" : ObjectId("5ea6e38a551299a9f98c93aa"), "Name" : "John Doe" }
Following is the query to perform multiple atomic updates using MongoDB −
> db.demo699.update({Name:"Chris Brown"},{ $set : { Name: "Adam Smith"} }, false, true ); WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
Display all documents from a collection with the help of find() method −
> db.demo699.find();
This will produce the following output −
{ "_id" : ObjectId("5ea6e370551299a9f98c93a7"), "Name" : "Adam Smith" } { "_id" : ObjectId("5ea6e37a551299a9f98c93a8"), "Name" : "David Miller" } { "_id" : ObjectId("5ea6e381551299a9f98c93a9"), "Name" : "Adam Smith" } { "_id" : ObjectId("5ea6e38a551299a9f98c93aa"), "Name" : "John Doe" }
- Related Articles
- Perform multiple updates with bulk operations and update elements in an array in MongoDB
- Perform conditional upserts or updates in MongoDB
- MongoDB find by multiple array items using $in?
- How to delete multiple documents in MongoDB using deleteMany()?
- Range Sum Queries Without Updates using C++
- Delete data from a collection in MongoDB using multiple conditions?
- Send SMS updates to mobile phone using python
- Search multiple fields for multiple values in MongoDB?
- How to Update multiple documents in a MongoDB collection using Java?
- How to insert multiple document into a MongoDB collection using Java?
- In MongoDB, is using $in search faster than multiple single searches?
- Implement multiple conditions in MongoDB?
- Compare multiple properties in MongoDB?
- MongoDB aggregation with multiple keys
- MongoDB filter multiple sub-documents?

Advertisements