

- 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
How does MongoDB Update() method work to set records of entire field?
You can use $set operator along with update(). Let us first create a collection with documents −
> db.workingOfUpdateMethod.insertOne({"ClientCountryName" : "AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd506fe2cba06f46efe9efa") } > db.workingOfUpdateMethod.insertOne({"ClientCountryName" : "AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd507022cba06f46efe9efb") } > db.workingOfUpdateMethod.insertOne({"ClientCountryName" : "AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd507022cba06f46efe9efc") } > db.workingOfUpdateMethod.insertOne({"ClientCountryName" : "AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd507032cba06f46efe9efd") }
Following is the query to display all documents from a collection with the help of find() method −
> db.workingOfUpdateMethod.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd506fe2cba06f46efe9efa"), "ClientCountryName" : "AUS" } { "_id" : ObjectId("5cd507022cba06f46efe9efb"), "ClientCountryName" : "AUS" } { "_id" : ObjectId("5cd507022cba06f46efe9efc"), "ClientCountryName" : "AUS" } { "_id" : ObjectId("5cd507032cba06f46efe9efd"), "ClientCountryName" : "AUS" }
Following is the query to update and set an entire field. Here, we are updating the field “ClientCountryName” −
> db.workingOfUpdateMethod.update( ... {"ClientCountryName" : "AUS"}, ... {$set: {"ClientCountryName" : "UK"}}, ... {multi: true}); WriteResult({ "nMatched" : 4, "nUpserted" : 0, "nModified" : 4 })
Let us check the all the documents once again −
> db.workingOfUpdateMethod.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd506fe2cba06f46efe9efa"), "ClientCountryName" : "UK" } { "_id" : ObjectId("5cd507022cba06f46efe9efb"), "ClientCountryName" : "UK" } { "_id" : ObjectId("5cd507022cba06f46efe9efc"), "ClientCountryName" : "UK" } { "_id" : ObjectId("5cd507032cba06f46efe9efd"), "ClientCountryName" : "UK" }
- Related Questions & Answers
- Update MongoDB field using value of another field?
- How to update _id field in MongoDB?
- Update _id field in MongoDB
- Update tag records in MongoDB quickly
- How does jQuery replaceWith() method work?
- How to query for records where field is null or not set in MongoDB?
- How does an FM radio set work?
- Want to update inner field in a MongoDB
- MongoDB query to update array with another field?
- How does jQuery.slice() method work in jQuery?
- How does jQuery.filter() method work in jQuery?
- How does jQuery.find() method work in jQuery?
- How does jQuery.eq() method work in jQuery?
- How does jQuery.map() method work in jQuery?
- How does jQuery.clone() method work in jQuery?
Advertisements