- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get the maximum mark records from a collection with documents in MongoDB query
To get the maximum marks records, use sort() in descending order along with limit 1. Let us create a collection with documents −
> db.demo241.insertOne({"Marks":67}); { "acknowledged" : true, "insertedId" : ObjectId("5e441f729af932883c61ea40") } > db.demo241.insertOne({"Marks":89}); { "acknowledged" : true, "insertedId" : ObjectId("5e441f769af932883c61ea41") } > db.demo241.insertOne({"Marks":78}); { "acknowledged" : true, "insertedId" : ObjectId("5e441f789af932883c61ea42") }
Display all documents from a collection with the help of find() method −
> db.demo241.find();
This will produce the following output −
{ "_id" : ObjectId("5e441f729af932883c61ea40"), "Marks" : 67 } { "_id" : ObjectId("5e441f769af932883c61ea41"), "Marks" : 89 } { "_id" : ObjectId("5e441f789af932883c61ea42"), "Marks" : 78 }
Following is the query to get the maximum marks −
> db.demo241.find().sort({Marks:-1}).limit(1);
This will produce the following output −
{ "_id" : ObjectId("5e441f769af932883c61ea41"), "Marks" : 89 }
- Related Articles
- Get the maximum mark records from a collection with documents in MongoDB
- MongoDB query to get minimum and maximum value from documents including some duplicate records
- MongoDB query to retrieve records from a collection named with letters and numbers
- Get MongoDB documents with max attribute per group in a collection?
- Get all embedded documents with “isMarried” status in a MongoDB collection
- Find a value in lowercase from a MongoDB collection with documents
- MongoDB query to update each field of documents in collection with a formula?
- MongoDB query to get distinct FirstName values from documents
- Get the maximum element in MongoDB collection?
- Evaluate one of more values from a MongoDB collection with documents
- Get the count of the number of documents in a MongoDB Collection?
- How to delete documents from a collection in MongoDB?
- How to retrieve documents from a collection in MongoDB?
- Get the size of all the documents in a MongoDB query?
- How to delete all the documents from a collection in MongoDB?

Advertisements