- 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
How to find minimum value in MongoDB?
To find the minimum value in MongoDB, you can use sort() along with limit(1). The syntax is as follows −
db.yourCollectionName.find().sort({yourFieldName: 1}).limit(1);
To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.findMinValueDemo.insertOne({"StudentMarks":78}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f80ea2f684a30fbdfd59f") } > db.findMinValueDemo.insertOne({"StudentMarks":69}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f80f02f684a30fbdfd5a0") } > db.findMinValueDemo.insertOne({"StudentMarks":79}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f80f32f684a30fbdfd5a1") } > db.findMinValueDemo.insertOne({"StudentMarks":59}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f80f82f684a30fbdfd5a2") } > db.findMinValueDemo.insertOne({"StudentMarks":91}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f80fb2f684a30fbdfd5a3") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.findMinValueDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8f80ea2f684a30fbdfd59f"), "StudentMarks" : 78 } { "_id" : ObjectId("5c8f80f02f684a30fbdfd5a0"), "StudentMarks" : 69 } { "_id" : ObjectId("5c8f80f32f684a30fbdfd5a1"), "StudentMarks" : 79 } { "_id" : ObjectId("5c8f80f82f684a30fbdfd5a2"), "StudentMarks" : 59 } { "_id" : ObjectId("5c8f80fb2f684a30fbdfd5a3"), "StudentMarks" : 91 }
Here is the query to find the minimum value in MongoDB −
> db.findMinValueDemo.find().sort({StudentMarks: 1}).limit(1);
The following is the output −
{ "_id" : ObjectId("5c8f80f82f684a30fbdfd5a2"), "StudentMarks" : 59 }
- Related Articles
- Find minimum value with MongoDB?
- Get maximum and minimum value in MongoDB?
- How to find the minimum value of an array in JavaScript?
- How to specify a minimum value in HTML?
- How to print NumberLong value in MongoDB?
- Find minimum unused value in a MySQL table?
- Find value above a specific value in MongoDB documents?
- How to add a field with static value to MongoDB find query?
- Swift Program to Find Minimum Key-Value Pair in the Dictionary
- MongoDB query to get minimum and maximum value from documents including some duplicate records
- How to find the element from a Python list with a minimum value?
- How to print document value in MongoDB shell?
- How to pop a single value in MongoDB?
- C++ Program to Find Minimum Value of any Algebraic Expression
- Program to find minimum possible maximum value after k operations in python

Advertisements