

- 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
MongoDB findOneAndUpdate() to update a single document
The findOneAndUpdate() is used to update only a single document in MongoDB. Let us create a collection with documents −
db.demo349.insertOne({"Name":"Chris","Marks":56}); { "acknowledged" : true, "insertedId" : ObjectId("5e55384af8647eb59e5620b4") } > db.demo349.insertOne({"Name":"David","Marks":78}); { "acknowledged" : true, "insertedId" : ObjectId("5e553853f8647eb59e5620b5") } > db.demo349.insertOne({"Name":"Chris","Marks":89}); { "acknowledged" : true, "insertedId" : ObjectId("5e55385af8647eb59e5620b6") } > db.demo349.insertOne({"Name":"David","Marks":54}); { "acknowledged" : true, "insertedId" : ObjectId("5e55385ff8647eb59e5620b7") }
Display all documents from a collection with the help of find() method −
> db.demo349.find();
This will produce the following output −
{ "_id" : ObjectId("5e55384af8647eb59e5620b4"), "Name" : "Chris", "Marks" : 56 } { "_id" : ObjectId("5e553853f8647eb59e5620b5"), "Name" : "David", "Marks" : 78 } { "_id" : ObjectId("5e55385af8647eb59e5620b6"), "Name" : "Chris", "Marks" : 89 } { "_id" : ObjectId("5e55385ff8647eb59e5620b7"), "Name" : "David", "Marks" : 54 }
Following is the query to work with findOneAndUpdate in MongoDB −
> db.demo349.findOneAndUpdate({ "Name" : "David" },{$inc:{Marks:10}}); { "_id" : ObjectId("5e553853f8647eb59e5620b5"), "Name" : "David", "Marks" : 78 }
Display all documents from a collection with the help of find() method −
> db.demo349.find();
This will produce the following output −
{ "_id" : ObjectId("5e55384af8647eb59e5620b4"), "Name" : "Chris", "Marks" : 56 } { "_id" : ObjectId("5e553853f8647eb59e5620b5"), "Name" : "David", "Marks" : 88 } { "_id" : ObjectId("5e55385af8647eb59e5620b6"), "Name" : "Chris", "Marks" : 89 } { "_id" : ObjectId("5e55385ff8647eb59e5620b7"), "Name" : "David", "Marks" : 54 }
- Related Questions & Answers
- Using findOneAndUpdate () to update in MongoDB?
- Update only a single document in MongoDB
- Update a single list item of a MongoDB document?
- Update only a single MongoDB document without deleting any date
- MongoDB query to update nested document
- Remove only a single document in MongoDB
- MongoDB query to update the nested document?
- How to update the _id of a MongoDB Document?
- MongoDB query to update a specific document from a collection
- Update only a specific value in a MongoDB document
- Increment only a single value in MongoDB document?
- How do you update a MongoDB document while replacing the entire document?
- Update a MongoDB document with Student Id and Name
- Update multiple rows in a single MongoDB query?
- How to update a MongoDB document without overwriting the existing one?
Advertisements