
- 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 match and group array elements with the max value in MongoDB aggregation?
For this, use $group along with $max in MongoDB. Let us create a collection with documents −
> db.demo510.insertOne( ... { ... details:[ ... { ... Name:"Chris", ... Score:56 ... }, ... { ... Name:"David", ... Score:45 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e8845fa987b6e0e9d18f582") } > db.demo510.insertOne( ... { ... details:[ ... { ... Name:"Chris", ... Score:56 ... }, ... { ... Name:"David", ... Score:47 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e8845fb987b6e0e9d18f583") } > db.demo510.insertOne( ... { ... details:[ ... { ... Name:"Chris", ... Score:45 ... }, ... { ... Name:"David", ... Score:91 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e8845fb987b6e0e9d18f584") }
Display all documents from a collection with the help of find() method −
> db.demo510.find();
This will produce the following output −
{ "_id" : ObjectId("5e8845fa987b6e0e9d18f582"), "details" : [ { "Name" : "Chris", "Score" : 56 }, { "Name" : "David", "Score" : 45 } ] } { "_id" : ObjectId("5e8845fb987b6e0e9d18f583"), "details" : [ { "Name" : "Chris", "Score" : 56 }, { "Name" : "David", "Score" : 47 } ] } { "_id" : ObjectId("5e8845fb987b6e0e9d18f584"), "details" : [ { "Name" : "Chris", "Score" : 45 }, { "Name" : "David", "Score" : 91 } ] }
Following is the query to match and group array elements with the max value in aggregation −
> db.demo510.aggregate([ ... { "$project": { ... "details": { ... "$arrayElemAt": [ ... { "$filter": { ... "input": "$details", ... "as": "res", ... "cond": { ... "$eq": [ ... "$$res.Score", ... { "$max": { ... "$map": { ... "input": "$details", ... "as": "out", ... "in": "$$out.Score" ... } ... }} ... ] ... } ... }}, ... 0 ... ] ... } ... }}, ... { "$group": { ... "_id": "$details.Name", ... "Name": { "$first": "$details.Name" }, ... "count": { "$sum": 1 } ... }} ... ])
This will produce the following output −
{ "_id" : "David", "Name" : "David", "count" : 1 } { "_id" : "Chris", "Name" : "Chris", "count" : 2 }
- Related Articles
- How to group nested fields in MongoDB aggregation with count value in array?
- Perform min/max with MongoDB aggregation
- MongoDB aggregation group and remove duplicate array values?
- Working with Aggregation to match all the values in MongoDB
- Does aggregation query with $match works in MongoDB?
- MongoDB query (aggregation framework) to match a specific field value
- MongoDB aggregation framework with group query example?
- MongoDB query to replace value with aggregation?
- Sort and Group in one MongoDB aggregation query?
- Match between fields in MongoDB aggregation framework?
- MongoDB aggregation with equality inside array?
- Get Absolute value with MongoDB aggregation framework?
- MongoDB aggregation framework match OR is possible?
- How to add together a subset of elements of an array in MongoDB aggregation?
- MongoDB aggregation to fetch documents with specific field value?

Advertisements