
- 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 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 Articles
- How to update _id field in MongoDB?
- Update MongoDB field using value of another field?
- How to query for records where field is null or not set in MongoDB?
- Update _id field in MongoDB
- Update tag records in MongoDB quickly
- MongoDB query to update array with another field?
- Want to update inner field in a MongoDB
- How do you update a MongoDB document while replacing the entire document?
- How to run MongoDB query to update only a specific field value?
- How does jQuery replaceWith() method work?
- How to update a single field in a capped collection in MongoDB?
- How does an FM radio set work?
- Convert a field to an array using MongoDB update operation?
- Conditional update depending on field matched in MongoDB
- Update field in exact element array in MongoDB?

Advertisements