
- 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 all documents in MongoDB?
You can use updateMany() to update documents. Let us create a collection with a document. The query to create a collection with a document is as follows −
> db.updateManyDocumentsDemo.insertOne({"StudentName":"John","StudentLastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5c948edd4cf1f7a64fa4df48") } > db.updateManyDocumentsDemo.insertOne({"StudentName":"John","StudentLastName":"Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5c948ee64cf1f7a64fa4df49") } > db.updateManyDocumentsDemo.insertOne({"StudentName":"Carol","StudentLastName":"Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5c948ef14cf1f7a64fa4df4a") } > db.updateManyDocumentsDemo.insertOne({"StudentName":"David","StudentLastName":"Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5c948f044cf1f7a64fa4df4b") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.updateManyDocumentsDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c948edd4cf1f7a64fa4df48"), "StudentName" : "John", "StudentLastName" : "Smith" } { "_id" : ObjectId("5c948ee64cf1f7a64fa4df49"), "StudentName" : "John", "StudentLastName" : "Doe" } { "_id" : ObjectId("5c948ef14cf1f7a64fa4df4a"), "StudentName" : "Carol", "StudentLastName" : "Taylor" } { "_id" : ObjectId("5c948f044cf1f7a64fa4df4b"), "StudentName" : "David", "StudentLastName" : "Miller" }
Here is the query to update all documents. The “StudentName” is updated with “StudentFirstName” −
> db.updateManyDocumentsDemo.updateMany({}, {$rename: {'StudentName': "StudentFirstName"}});
The following is the output −
{ "acknowledged" : true, "matchedCount" : 4, "modifiedCount" : 4 }
Check the document has been updated or not. The query is as follows −
> db.updateManyDocumentsDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c948edd4cf1f7a64fa4df48"), "StudentLastName" : "Smith", "StudentFirstName" : "John" } { "_id" : ObjectId("5c948ee64cf1f7a64fa4df49"), "StudentLastName" : "Doe", "StudentFirstName" : "John" } { "_id" : ObjectId("5c948ef14cf1f7a64fa4df4a"), "StudentLastName" : "Taylor", "StudentFirstName" : "Carol" } { "_id" : ObjectId("5c948f044cf1f7a64fa4df4b"), "StudentLastName" : "Miller", "StudentFirstName" : "David" }
- Related Articles
- How to update multiple documents in MongoDB?
- MongoDB query to update all documents matching specific IDs
- How to validate documents before insert or update in MongoDB?
- How to update many documents with one query in MongoDB?
- How to Update multiple documents in a MongoDB collection using Java?
- MongoDB - update partial number of documents?
- How to update or modify the existing documents of a collection in MongoDB?
- Update objects in a MongoDB documents array (nested updating)?
- Update values in multiple documents with multi parameter in MongoDB?
- How to find latest entries in array over all MongoDB documents?
- How to delete all the documents from a collection in MongoDB?
- MongoDB query to update each field of documents in collection with a formula?
- Group all documents with common fields in MongoDB?
- How can I rename a field for all documents in MongoDB?
- Fetch all the ids of MongoDB documents?

Advertisements