
- 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
Perform min/max with MongoDB aggregation
For min/max in MongoDB, use $min and $max. Let us create a collection with documents −
> db.demo251.insertOne({"Marks":78}); { "acknowledged" : true, "insertedId" : ObjectId("5e46c0001627c0c63e7dba74") } > db.demo251.insertOne({"Marks":87}); { "acknowledged" : true, "insertedId" : ObjectId("5e46c0031627c0c63e7dba75") } > db.demo251.insertOne({"Marks":56}); { "acknowledged" : true, "insertedId" : ObjectId("5e46c0061627c0c63e7dba76") } > db.demo251.insertOne({"Marks":76}); { "acknowledged" : true, "insertedId" : ObjectId("5e46c00c1627c0c63e7dba77") }
Display all documents from a collection with the help of find() method −
> db.demo251.find();
This will produce the following output −
{ "_id" : ObjectId("5e46c0001627c0c63e7dba74"), "Marks" : 78 } { "_id" : ObjectId("5e46c0031627c0c63e7dba75"), "Marks" : 87 } { "_id" : ObjectId("5e46c0061627c0c63e7dba76"), "Marks" : 56 } { "_id" : ObjectId("5e46c00c1627c0c63e7dba77"), "Marks" : 76 }
Following is the query to implement min/max aggregation in MongoDB −
> db.demo251.aggregate([ ... { "$group": { ... "_id": null, ... "MaxMarks": { "$max": "$Marks" }, ... "MinMarks": { "$min": "$Marks" } ... }} ...])
This will produce the following output −
{ "_id" : null, "MaxMarks" : 87, "MinMarks" : 56 }
- Related Articles
- Perform aggregation sort in MongoDB?
- Min and max values of an array in MongoDB?
- How to match and group array elements with the max value in MongoDB aggregation?
- Min-Max Heaps
- MongoDB aggregation with multiple keys
- Symmetric Min-Max Heaps
- MongoDB aggregation with equality inside array?
- max() and min() in Python
- Get Absolute value with MongoDB aggregation framework?
- MongoDB query to replace value with aggregation?
- How to use $ifNull with MongoDB aggregation?
- MongoDB aggregation framework with group query example?
- Count by multiple fields with MongoDB aggregation
- Does aggregation query with $match works in MongoDB?
- Box plot with min, max, average and standard deviation in Matplotlib

Advertisements