- 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
Conditional update depending on field matched in MongoDB
For conditional update, use update() and set new value using $set. Let us create a collection with documents −
> db.demo150.insertOne({"StudentId":101,"StudentName":"Chris","StudentMarks":35}); { "acknowledged" : true, "insertedId" : ObjectId("5e350dcdfdf09dd6d08539d3") } > db.demo150.insertOne({"StudentId":102,"StudentName":"Chris","StudentMarks":55}); { "acknowledged" : true, "insertedId" : ObjectId("5e350dcefdf09dd6d08539d4") } > db.demo150.insertOne({"StudentId":103,"StudentName":"David","StudentMarks":34}); { "acknowledged" : true, "insertedId" : ObjectId("5e350dcffdf09dd6d08539d5") } > db.demo150.insertOne({"StudentId":104,"StudentName":"Chris","StudentMarks":38}); { "acknowledged" : true, "insertedId" : ObjectId("5e350dd0fdf09dd6d08539d6") }
Display all documents from a collection with the help of find() method −
> db.demo150.find();
This will produce the following output −
{ "_id" : ObjectId("5e350dcdfdf09dd6d08539d3"), "StudentId" : 101, "StudentName" : "Chris", "StudentMarks" : 35 } { "_id" : ObjectId("5e350dcefdf09dd6d08539d4"), "StudentId" : 102, "StudentName" : "Chris", "StudentMarks" : 55 } { "_id" : ObjectId("5e350dcffdf09dd6d08539d5"), "StudentId" : 103, "StudentName" : "David", "StudentMarks" : 34 } { "_id" : ObjectId("5e350dd0fdf09dd6d08539d6"), "StudentId" : 104, "StudentName" : "Chris", "StudentMarks" : 38 }
Following is the query for conditional update depending on field matched −
> db.demo150.update({"StudentId":103},{$set:{"StudentMarks":97}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo150.find();
This will produce the following output −
{ "_id" : ObjectId("5e350dcdfdf09dd6d08539d3"), "StudentId" : 101, "StudentName" : "Chris", "StudentMarks" : 35 } { "_id" : ObjectId("5e350dcefdf09dd6d08539d4"), "StudentId" : 102, "StudentName" : "Chris", "StudentMarks" : 55 } { "_id" : ObjectId("5e350dcffdf09dd6d08539d5"), "StudentId" : 103, "StudentName" : "David", "StudentMarks" : 97 } { "_id" : ObjectId("5e350dd0fdf09dd6d08539d6"), "StudentId" : 104, "StudentName" : "Chris", "StudentMarks" : 38 }
- Related Articles
- How to do conditional update in MongoDB?
- Update _id field in MongoDB
- Update MongoDB field using value of another field?
- How to update _id field in MongoDB?
- Update field in exact element array in MongoDB?
- Want to update inner field in a MongoDB
- MongoDB query to update array with another field?
- Convert a field to an array using update operation in MongoDB
- Convert a field to an array using MongoDB update operation?
- How to update a single field in a capped collection in MongoDB?
- Update quantity in MongoDB based on two conditions?
- MongoDB query to update field and modify the data currently in column
- Update salary field value with 10 percent of each employee in MongoDB
- Search a sub-field on MongoDB?
- Update all the values of a field with a specific string in MongoDB?

Advertisements