
- 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
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 Articles
- 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
- 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
- Remove only a single document in MongoDB
- Update only a specific value in a MongoDB document
- How do you update a MongoDB document while replacing the entire document?
- Increment only a single value in MongoDB document?
- Update a MongoDB document with Student Id and Name
- How to update a MongoDB document without overwriting the existing one?
- Update multiple rows in a single MongoDB query?

Advertisements