
- 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
Update only a specific value in a MongoDB document
To update only a specific value, use update() and in that set the new value using $set. Let us create a collection with documents −
> db.demo201.insertOne({"ClientName":"Chris Brown","ClientAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5e3c39ca03d395bdc21346e5") } > db.demo201.insertOne({"ClientName":"David Miller","ClientAge":35}); { "acknowledged" : true, "insertedId" : ObjectId("5e3c39d403d395bdc21346e6") } > db.demo201.insertOne({"ClientName":"Carol Taylor","ClientAge":28}); { "acknowledged" : true, "insertedId" : ObjectId("5e3c39e603d395bdc21346e7") }
Display all documents from a collection with the help of find() method −
> db.demo201.find();
This will produce the following output −
{ "_id" : ObjectId("5e3c39ca03d395bdc21346e5"), "ClientName" : "Chris Brown", "ClientAge" : 26 } { "_id" : ObjectId("5e3c39d403d395bdc21346e6"), "ClientName" : "David Miller", "ClientAge" : 35 } { "_id" : ObjectId("5e3c39e603d395bdc21346e7"), "ClientName" : "Carol Taylor", "ClientAge" : 28 }
Following is the query to update MongoDB −
> db.demo201.update({"ClientAge":35},{$set:{"ClientName":"John Doe"}},{multi:true}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo201.find();
This will produce the following output −
{ "_id" : ObjectId("5e3c39ca03d395bdc21346e5"), "ClientName" : "Chris Brown", "ClientAge" : 26 } { "_id" : ObjectId("5e3c39d403d395bdc21346e6"), "ClientName" : "John Doe", "ClientAge" : 35 } { "_id" : ObjectId("5e3c39e603d395bdc21346e7"), "ClientName" : "Carol Taylor", "ClientAge" : 28 }
- Related Articles
- Update only a single document in MongoDB
- MongoDB query to update a specific document from a collection
- How to run MongoDB query to update only a specific field value?
- Update only specific fields in MongoDB?
- Increment only a single value in MongoDB document?
- Update only a single MongoDB document without deleting any date
- Update a specific MongoDB document in array with $set and positional $ operator?
- MongoDB query to pull a specific value from a document
- Find document with array that contains a specific value in MongoDB
- MongoDB query select and display only a specific field from the document?
- Only insert if a value is unique in MongoDB else update
- Find the document by field name with a specific value in MongoDB?
- MongoDB findOneAndUpdate() to update a single document
- Remove only a single document in MongoDB
- Extract a MongoDB document with a specific string

Advertisements