

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- List all values of a certain field in MongoDB?
- Find all collections in MongoDB with specific field?
- Find all the non-distinct values of a field in MongoDB?
- Fetch specific field values in MongoDB
- MongoDB query to add up the values of a specific field in documents
- Limit number of values in a field with MongoDB?
- Return a specific field in MongoDB?
- How to run MongoDB query to update only a specific field value?
- Get the duplicate values of a field in MongoDB?
- Update object in array with a specific key in MongoDB
- Extract a MongoDB document with a specific string
- Want to update inner field in a MongoDB
- Update MongoDB field using value of another field?
- Update only a specific value in a MongoDB document
- Find the document by field name with a specific value in MongoDB?
Advertisements