
- 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
Using findOneAndUpdate () to update in MongoDB?
The findOneAndUpdate() is used to update a single document based on the filter and sort criteria i.e. −
db.collection.findOneAndUpdate(filter, update, options)
Let us create a collection with documents −
> db.demo328.insertOne({Name:"Chris",Marks:67}); { "acknowledged" : true, "insertedId" : ObjectId("5e516b19f8647eb59e56207a") } > db.demo328.insertOne({Name:"David",Marks:78}); { "acknowledged" : true, "insertedId" : ObjectId("5e516b24f8647eb59e56207b") } > db.demo328.insertOne({Name:"Bob",Marks:97}); { "acknowledged" : true, "insertedId" : ObjectId("5e516b2bf8647eb59e56207c") }
Display all documents from a collection with the help of find() method −
> db.demo328.find();
This will produce the following output −
{ "_id" : ObjectId("5e516b19f8647eb59e56207a"), "Name" : "Chris", "Marks" : 67 } { "_id" : ObjectId("5e516b24f8647eb59e56207b"), "Name" : "David", "Marks" : 78 } { "_id" : ObjectId("5e516b2bf8647eb59e56207c"), "Name" : "Bob", "Marks" : 97 }
Following is the query to update with findOneAndUpdate(). Here, we are incrementing a specific document’s field Marks −
> db.demo328.findOneAndUpdate({Name:"David"},{ $inc: { "Marks" : 10} }); { "_id" : ObjectId("5e516b24f8647eb59e56207b"), "Name" : "David", "Marks" : 78 }
Display all documents from a collection with the help of find() method −
> db.demo328.find();
This will produce the following output −
{ "_id" : ObjectId("5e516b19f8647eb59e56207a"), "Name" : "Chris", "Marks" : 67 } { "_id" : ObjectId("5e516b24f8647eb59e56207b"), "Name" : "David", "Marks" : 88 } { "_id" : ObjectId("5e516b2bf8647eb59e56207c"), "Name" : "Bob", "Marks" : 97 }
- Related Articles
- MongoDB findOneAndUpdate() to update a single document
- How to update MongoDB collection using $toLower?
- MongoDB query to update an array using FindAndUpdate()?
- Convert a field to an array using update operation in MongoDB
- How to update an existing document in MongoDB collection using Java?
- How to Update multiple documents in a MongoDB collection using Java?
- Convert a field to an array using MongoDB update operation?
- Update MongoDB field using value of another field?
- How to update MongoDB Object?
- MongoDB query to update tag
- How to update all documents in MongoDB?
- How to update multiple documents in MongoDB?
- How to update _id field in MongoDB?
- How to update after aggregate in MongoDB?
- How to do conditional update in MongoDB?

Advertisements