
- 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
How to get max values for distinct elements in MongoDB
To get max values for distinct elements, use $sort and $group in MongoDB aggregate(). Let us create a collection with documents −
> db.demo750.insertOne({id:101,value:50}); { "acknowledged" : true, "insertedId" : ObjectId("5eae74b2a930c785c834e566") } > db.demo750.insertOne({id:102,value:40}); { "acknowledged" : true, "insertedId" : ObjectId("5eae74c8a930c785c834e567") } > db.demo750.insertOne({id:101,value:110}); { "acknowledged" : true, "insertedId" : ObjectId("5eae74dba930c785c834e568") }
Display all documents from a collection with the help of find() method −
> db.demo750.find();
This will produce the following output −
{ "_id" : ObjectId("5eae74b2a930c785c834e566"), "id" : 101, "value" : 50 } { "_id" : ObjectId("5eae74c8a930c785c834e567"), "id" : 102, "value" : 40 } { "_id" : ObjectId("5eae74dba930c785c834e568"), "id" : 101, "value" : 110 }
Following is the query to get max values for distinct elements in MongoDB −
> db.demo750.aggregate([ ... { ... $sort: { value: -1 } ... }, ... { ... $group: { ... _id: "$id", ... value: { $first: "$value" } ... } ... }, ... { ... $project: { ... id: "$_id", ... value: 1 ... } ... } ... ])
This will produce the following output −
{ "_id" : 102, "value" : 40, "id" : 102 } { "_id" : 101, "value" : 110, "id" : 101 }
- Related Articles
- Get distinct record values in MongoDB?
- MongoDB query to get only distinct values
- Get Distinct Values with Sorted Data in MongoDB?
- Get distinct values from a column in MongoDB?
- How to get distinct list of sub-document field values in MongoDB?
- MongoDB query to get distinct FirstName values from documents
- How to get element with max id in MongoDB?
- How to get distinct values for non-key column fields in Laravel?
- Get the length of distinct values in an array with MongoDB
- How to get the max of two values MySQL?
- MongoDB query for counting the distinct values across all documents?
- How to get values of cursor in MongoDB?
- How to count number of distinct values per field/ key in MongoDB?
- Getting distinct values from object array in MongoDB?
- Min and max values of an array in MongoDB?

Advertisements