
- 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 values in multiple documents with multi parameter in MongoDB?
You need to set multi to true. Include the option multi − true to update all documents that match the query criteria.
Let us first create a collection with documents −
> db.demo390.insertOne({"FirstName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e5d1f3a22064be7ab44e7fa") } > db.demo390.insertOne({"FirstName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e5d1f3e22064be7ab44e7fb") } > db.demo390.insertOne({"FirstName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e5d1f4122064be7ab44e7fc") } > db.demo390.insertOne({"FirstName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e5d1f4422064be7ab44e7fd") }
Display all documents from a collection with the help of find() method −
> db.demo390.find();
This will produce the following output −
{ "_id" : ObjectId("5e5d1f3a22064be7ab44e7fa"), "FirstName" : "Chris" } { "_id" : ObjectId("5e5d1f3e22064be7ab44e7fb"), "FirstName" : "David" } { "_id" : ObjectId("5e5d1f4122064be7ab44e7fc"), "FirstName" : "Bob" } { "_id" : ObjectId("5e5d1f4422064be7ab44e7fd"), "FirstName" : "Chris" }
Following is the query to update values with multi − true −
> db.demo390.update({FirstName:"Chris"},{$set:{"FirstName":"John"}},{multi:true}); WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
Display all documents from a collection with the help of find() method −
> db.demo390.find();
This will produce the following output −
{ "_id" : ObjectId("5e5d1f3a22064be7ab44e7fa"), "FirstName" : "John" } { "_id" : ObjectId("5e5d1f3e22064be7ab44e7fb"), "FirstName" : "David" } { "_id" : ObjectId("5e5d1f4122064be7ab44e7fc"), "FirstName" : "Bob" } { "_id" : ObjectId("5e5d1f4422064be7ab44e7fd"), "FirstName" : "John" }
- Related Articles
- How to update multiple documents in MongoDB?
- How to Update multiple documents in a MongoDB collection using Java?
- Resolve ‘multi update only works with $ operators’ in MongoDb?
- How to update many documents with one query in MongoDB?
- Fetch multiple documents in MongoDB query with OR condition?
- How to update all documents in MongoDB?
- Search for multiple documents in MongoDB?
- Fetch specific multiple documents in MongoDB
- Update with multiple values in MySQL WHERE clause
- How to update array with multiple conditions in MongoDB
- How to merge multiple documents in MongoDB?
- MongoDB - update partial number of documents?
- MongoDB filter multiple sub-documents?
- Ignore null values in MongoDB documents
- MongoDB query to get documents with multiple conditions set in $or?

Advertisements