- 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
How to run MongoDB query to update only a specific field value?
Let us see an example and create a collection with documents −
> db.demo557.insertOne({Name:"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f28e954b4472ed3e8e864") } > db.demo557.insertOne({Name:"David"});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f28ee54b4472ed3e8e865") }
Display all documents from a collection with the help of find() method −
> db.demo557.find();
This will produce the following output −
{ "_id" : ObjectId("5e8f28e954b4472ed3e8e864"), "Name" : "Chris" } { "_id" : ObjectId("5e8f28ee54b4472ed3e8e865"), "Name" : "David" }
Following is the query to update only a specific field value −
> db.getCollection('demo557').update({Name:"Chris"},{$set:{Name:"Robert"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo557.find();
This will produce the following output −
{ "_id" : ObjectId("5e8f28e954b4472ed3e8e864"), "Name" : "Robert" } { "_id" : ObjectId("5e8f28ee54b4472ed3e8e865"), "Name" : "David" }
- Related Articles
- MongoDB query (aggregation framework) to match a specific field value
- Update only a specific value in a MongoDB document
- MongoDB query to update only certain fields?
- MongoDB query to update a MongoDB row with only the objectid
- MongoDB query to update array with another field?
- MongoDB query to update a specific document from a collection
- MongoDB query select and display only a specific field from the document?
- Update only specific fields in MongoDB?
- How to return only value of a field in MongoDB?
- MongoDB query to update all documents matching specific IDs
- MongoDB query to get only a specific number of elements
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- MySQL query to update only a single field in place of NULL
- How to add a field with static value to MongoDB find query?
- MongoDB Query to search for records only in a specific hour?

Advertisements