
- 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
MongoDB query to get minimum and maximum value from documents including some duplicate records
For this, use aggregate() and $group. To get minimum and maximum value, use $min and $max.
Let us create a collection with documents −
> db.demo167.insertOne({"Score":60}); { "acknowledged" : true, "insertedId" : ObjectId("5e3693a79e4f06af551997d1") } > db.demo167.insertOne({"Score":80}); { "acknowledged" : true, "insertedId" : ObjectId("5e3693ab9e4f06af551997d2") } > db.demo167.insertOne({"Score":60}); { "acknowledged" : true, "insertedId" : ObjectId("5e3693ad9e4f06af551997d3") } > db.demo167.insertOne({"Score":90}); { "acknowledged" : true, "insertedId" : ObjectId("5e3693b09e4f06af551997d4") } > db.demo167.insertOne({"Score":89}); { "acknowledged" : true, "insertedId" : ObjectId("5e3693b69e4f06af551997d5") }
Display all documents from a collection with the help of find() method −
> db.demo167.find();
This will produce the following output −
{ "_id" : ObjectId("5e3693a79e4f06af551997d1"), "Score" : 60 } { "_id" : ObjectId("5e3693ab9e4f06af551997d2"), "Score" : 80 } { "_id" : ObjectId("5e3693ad9e4f06af551997d3"), "Score" : 60 } { "_id" : ObjectId("5e3693b09e4f06af551997d4"), "Score" : 90 } { "_id" : ObjectId("5e3693b69e4f06af551997d5"), "Score" : 89 }
Following is the query to get minimum and maximum value −
> var d = [ ... { ... "$group": { ... "_id": "id", ... "MinimumValue": { "$min": "$Score" }, ... "MaximumValue": { "$max": "$Score" } ... } ... } ... ] > db.demo167.aggregate(d);
This will produce the following output −
{ "_id" : "id", "MinimumValue" : 60, "MaximumValue" : 90 }
- Related Articles
- Get the maximum mark records from a collection with documents in MongoDB query
- MongoDB query to group duplicate documents
- Get the maximum mark records from a collection with documents in MongoDB
- Get maximum and minimum value in MongoDB?
- MongoDB query to get distinct FirstName values from documents
- MySQL query to fetch the maximum corresponding value from duplicate column values
- How to get duplicate records from Android sqlite?
- MongoDB query to get date records in a range
- MongoDB query to get specific list of names from documents where the value of a field is an array
- MongoDB query to skip documents
- How to get items with a specific value from documents using MongoDB shell?
- MongoDB query to get only specific fields in nested array documents?
- MongoDB query to get documents with multiple conditions set in $or?
- MongoDB - Query embedded documents?
- Find duplicate records in MongoDB?

Advertisements