

- 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
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 Questions & Answers
- MongoDB query to update a MongoDB row with only the objectid
- Query MongoDB with length criteria?
- Match multiple criteria inside an array with MongoDB?
- Find value in a MongoDB Array with multiple criteria?
- Get the last two values with MongoDB
- MongoDB query to find and return subdocument with criteria?
- How to match multiple criteria inside an array with MongoDB?
- MongoDB query to find value in array with multiple criteria (range)
- MongoDB concurrent update with sub collection?
- MongoDB query with case insensitive search?
- Text search in MongoDB with Regular Expression
- Display the last two values from field with MongoDB
- Get all the MongoDB documents but not the ones with two given criteria’s?
- Update MongoDB variable value with variable itself?
- Update an entire row in MySQL?
Advertisements