- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MongoDB - update partial number of documents?
To update partial number of documents, set multi to true. Let us create a collection with documents −
> db.demo312.insertOne({"FirstName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5e50ce16f8647eb59e56204a") } > db.demo312.insertOne({"FirstName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e50ce19f8647eb59e56204b") } > db.demo312.insertOne({"FirstName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5e50ce1cf8647eb59e56204c") } > db.demo312.insertOne({"FirstName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e50ce20f8647eb59e56204d") } > db.demo312.insertOne({"FirstName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5e50ce22f8647eb59e56204e") }
Display all documents from a collection with the help of find() method −
> db.demo312.find();
This will produce the following output −
{ "_id" : ObjectId("5e50ce16f8647eb59e56204a"), "FirstName" : "Robert" } { "_id" : ObjectId("5e50ce19f8647eb59e56204b"), "FirstName" : "Bob" } { "_id" : ObjectId("5e50ce1cf8647eb59e56204c"), "FirstName" : "Robert" } { "_id" : ObjectId("5e50ce20f8647eb59e56204d"), "FirstName" : "David" } { "_id" : ObjectId("5e50ce22f8647eb59e56204e"), "FirstName" : "Robert" }
Following is the query to update partial number of documents
> db.demo312.update({"FirstName":"Robert"},{$set:{"FirstName":"Sam"}},{multi:true}); WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
Display all documents from a collection with the help of find() method −
> db.demo312.find();
This will produce the following output −
{ "_id" : ObjectId("5e50ce16f8647eb59e56204a"), "FirstName" : "Sam" } { "_id" : ObjectId("5e50ce19f8647eb59e56204b"), "FirstName" : "Bob" } { "_id" : ObjectId("5e50ce1cf8647eb59e56204c"), "FirstName" : "Sam" } { "_id" : ObjectId("5e50ce20f8647eb59e56204d"), "FirstName" : "David" } { "_id" : ObjectId("5e50ce22f8647eb59e56204e"), "FirstName" : "Sam" }
- Related Articles
- How to update all documents in MongoDB?
- How to update multiple documents in MongoDB?
- Update objects in a MongoDB documents array (nested updating)?
- MongoDB query to update all documents matching specific IDs
- Get number of updated documents in MongoDB?
- How to validate documents before insert or update in MongoDB?
- Update values in multiple documents with multi parameter in MongoDB?
- How to update many documents with one query in MongoDB?
- How to update or modify the existing documents of a collection in MongoDB?
- MongoDB query to update each field of documents in collection with a formula?
- How to Update multiple documents in a MongoDB collection using Java?
- Count number of documents from MongoDB collection inside Array?
- Delete partial data in MongoDB?
- Limit the number of documents in a collection in MongoDB?
- How to count the number of documents in a MongoDB collection?

Advertisements