
- 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
Min and max values of an array in MongoDB?
To get the min and max values, use $min and $max. Let us create a collection with documents −
> db.demo286.insertOne({"details":[{Value1:70,Value2:50},{Value1:30,Value2:36}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e4ac743f49383b52759cbbc") }
Display all documents from a collection with the help of find() method −
> db.demo286.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e4ac743f49383b52759cbbc"), "details" : [ { "Value1" : 70, "Value2" : 50 }, { "Value1" : 30, "Value2" : 36 } ] }
Here is the query to get min and max values of an array −
> db.demo286.aggregate([ { "$project": { "name": 1, "MinValue1": { "$min": "$details.Value1" }, "MaxValue2": { "$max": "$details.Value2" } }} ])
This will produce the following output −
{ "_id" : ObjectId("5e4ac743f49383b52759cbbc"), "MinValue1" : 30, "MaxValue2" : 50 }
- Related Articles
- Get max and min values of an array in Arduino
- Find max and min values in an array of primitives using Java
- Find max and min values in array of primitives using Java
- Perform min/max with MongoDB aggregation
- Average of array excluding min max JavaScript
- How to find the min/max element of an Array in JavaScript?
- max() and min() in Python
- Sort array based on min and max date in JavaScript?
- How to calculate average without max and min values in Excel?
- Min-Max Range Queries in Array in C++
- Use of min() and max() in Python
- Get MAX and MIN values along with their row id in MySQL?
- Set min-width and max-width of an element using CSS
- Set min-height and max-height of an element using CSS
- Minimum removals from array to make max – min

Advertisements