
- 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
Get maximum and minimum value in MongoDB?
Use $max and $min operator along with aggregate framework to get the maximum and minimum value. Let us first create a collection with documents −
> db.maxAndMinDemo.insertOne({"Value":98}); { "acknowledged" : true, "insertedId" : ObjectId("5cd698a357806ebf1256f129") } > db.maxAndMinDemo.insertOne({"Value":97}); { "acknowledged" : true, "insertedId" : ObjectId("5cd698af57806ebf1256f12a") } > db.maxAndMinDemo.insertOne({"Value":69}); { "acknowledged" : true, "insertedId" : ObjectId("5cd698b357806ebf1256f12b") } > db.maxAndMinDemo.insertOne({"Value":96}); { "acknowledged" : true, "insertedId" : ObjectId("5cd698b657806ebf1256f12c") } > db.maxAndMinDemo.insertOne({"Value":99}); { "acknowledged" : true, "insertedId" : ObjectId("5cd698b957806ebf1256f12d") }
Following is the query to display all documents from a collection with the help of find() method −
> db.maxAndMinDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd698a357806ebf1256f129"), "Value" : 98 } { "_id" : ObjectId("5cd698af57806ebf1256f12a"), "Value" : 97 } { "_id" : ObjectId("5cd698b357806ebf1256f12b"), "Value" : 69 } { "_id" : ObjectId("5cd698b657806ebf1256f12c"), "Value" : 96 } { "_id" : ObjectId("5cd698b957806ebf1256f12d"), "Value" : 99 }
Following is the query to max and min in MongoDB −
> db.maxAndMinDemo.aggregate([ { "$group": { "_id": null, "MaximumValue": { "$max": "$Value" }, "MinimumValue": { "$min": "$Value" } }} ]);
This will produce the following output −
{ "_id" : null, "MaximumValue" : 99, "MinimumValue" : 69 }
- Related Articles
- MongoDB query to get minimum and maximum value from documents including some duplicate records
- Getting Minimum and Maximum Value in MySQL
- Find minimum value with MongoDB?
- Path With Maximum Minimum Value in Python
- Get the minimum and maximum value from a VARCHAR column and display the result in separate MySQL columns?
- How to find minimum value in MongoDB?
- Get the maximum element in MongoDB collection?
- Java Program to Get Minimum and Maximum From a List
- Golang program to get maximum and minimum from a slice
- C++ Program to get difference between maximum and minimum water in barrels
- Display the minimum and maximum value of primitive data types in Java
- How can I get maximum and minimum values in a single MySQL query?
- How to read Maximum and Minimum temperature in Six's Maximum and Minimum Thermometer?
- Function that returns the minimum and maximum value of an array in JavaScript
- Get the minimum value in the SortedSet in C#

Advertisements