- 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
Update all the values of a field with a specific string in MongoDB?
To update all the values, use update() along with multi:true. Let us create a collection with documents −
> db.demo720.insertOne({"SubjectName":"MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5eaae7ca43417811278f5883") } > db.demo720.insertOne({"SubjectName":"Java"}); { "acknowledged" : true, "insertedId" : ObjectId("5eaae7ce43417811278f5884") } > db.demo720.insertOne({"SubjectName":"C"}); { "acknowledged" : true, "insertedId" : ObjectId("5eaae7d143417811278f5885") } > db.demo720.insertOne({"SubjectName":"C++"}); { "acknowledged" : true, "insertedId" : ObjectId("5eaae7d543417811278f5886") }
Display all documents from a collection with the help of find() method −
> db.demo720.find();
This will produce the following output −
{ "_id" : ObjectId("5eaae7ca43417811278f5883"), "SubjectName" : "MySQL" } { "_id" : ObjectId("5eaae7ce43417811278f5884"), "SubjectName" : "Java" } { "_id" : ObjectId("5eaae7d143417811278f5885"), "SubjectName" : "C" } { "_id" : ObjectId("5eaae7d543417811278f5886"), "SubjectName" : "C++" }
Following is the query to update all the values of the field “SubjectName” −
> db.demo720.update({},{$set:{SubjectName:"MongoDB"}},{multi:true}); WriteResult({ "nMatched" : 4, "nUpserted" : 0, "nModified" : 4 })
Display all documents from a collection with the help of find() method −
> db.demo720.find();
This will produce the following output −
{ "_id" : ObjectId("5eaae7ca43417811278f5883"), "SubjectName" : "MongoDB" } { "_id" : ObjectId("5eaae7ce43417811278f5884"), "SubjectName" : "MongoDB" } { "_id" : ObjectId("5eaae7d143417811278f5885"), "SubjectName" : "MongoDB" } { "_id" : ObjectId("5eaae7d543417811278f5886"), "SubjectName" : "MongoDB" }
- Related Articles
- Find all collections in MongoDB with specific field?
- Fetch specific field values in MongoDB
- List all values of a certain field in MongoDB?
- Find all the non-distinct values of a field in MongoDB?
- MongoDB query to add up the values of a specific field in documents
- How to run MongoDB query to update only a specific field value?
- Update object in array with a specific key in MongoDB
- Limit number of values in a field with MongoDB?
- Return a specific field in MongoDB?
- Extract a MongoDB document with a specific string
- MongoDB query to update each field of documents in collection with a formula?
- Find the document by field name with a specific value in MongoDB?
- Update only a specific value in a MongoDB document
- MongoDB query to update all documents matching specific IDs
- Want to update inner field in a MongoDB

Advertisements