
- 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
Update the last row with search criteria in MongoDB?
To update with search criteria, use findAndModify() in MongoDB. Let us create a collection with documents −
> db.demo516.insertOne({"Name":"John","Age":22,"Score":56});{ "acknowledged" : true, "insertedId" : ObjectId("5e889fdb987b6e0e9d18f591") } > db.demo516.insertOne({"Name":"John","Age":23,"Score":67});{ "acknowledged" : true, "insertedId" : ObjectId("5e889ff1987b6e0e9d18f592") } > db.demo516.insertOne({"Name":"John","Age":22,"Score":56});{ "acknowledged" : true, "insertedId" : ObjectId("5e889ff3987b6e0e9d18f593") } > db.demo516.insertOne({"Name":"John","Age":22,"Score":66});{ "acknowledged" : true, "insertedId" : ObjectId("5e889ffa987b6e0e9d18f594") }
Display all documents from a collection with the help of find() method −
> db.demo516.find();
This will produce the following output −
{ "_id" : ObjectId("5e889fdb987b6e0e9d18f591"), "Name" : "John", "Age" : 22, "Score" : 56 } { "_id" : ObjectId("5e889ff1987b6e0e9d18f592"), "Name" : "John", "Age" : 23, "Score" : 67 } { "_id" : ObjectId("5e889ff3987b6e0e9d18f593"), "Name" : "John", "Age" : 22, "Score" : 56 } { "_id" : ObjectId("5e889ffa987b6e0e9d18f594"), "Name" : "John", "Age" : 22, "Score" : 66 }
Following is the query to update the last row with search criteria in MongoDB −
> db.demo516.findAndModify({ ... query: {Name: "John", Age: 22}, ... sort: {_id: -1}, ... update: {$set: {Score: 98}}, ... new: true ... }) { "_id" : ObjectId("5e889ffa987b6e0e9d18f594"), "Name" : "John", "Age" : 22, "Score" : 98 }
Display all documents from a collection with the help of find() method −
> db.demo516.find();
This will produce the following output −
{ "_id" : ObjectId("5e889fdb987b6e0e9d18f591"), "Name" : "John", "Age" : 22, "Score" : 56 } { "_id" : ObjectId("5e889ff1987b6e0e9d18f592"), "Name" : "John", "Age" : 23, "Score" : 67 } { "_id" : ObjectId("5e889ff3987b6e0e9d18f593"), "Name" : "John", "Age" : 22, "Score" : 56 } { "_id" : ObjectId("5e889ffa987b6e0e9d18f594"), "Name" : "John", "Age" : 22, "Score" : 98 }
- Related Articles
- MongoDB query to update a MongoDB row with only the objectid
- Query MongoDB with length criteria?
- Find value in a MongoDB Array with multiple criteria?
- Match multiple criteria inside an array with MongoDB?
- MongoDB query to find and return subdocument with criteria?
- MongoDB query to find value in array with multiple criteria (range)
- Get the last two values with MongoDB
- How to match multiple criteria inside an array with MongoDB?
- Text search in MongoDB with Regular Expression
- MongoDB concurrent update with sub collection?
- MongoDB query with case insensitive search?
- Update two separate arrays in a document with one update call in MongoDB?
- Using a regex with text search in MongoDB
- Update MongoDB variable value with variable itself?
- Resolve ‘multi update only works with $ operators’ in MongoDb?

Advertisements