

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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?
- Sort and Group in one MongoDB aggregation query?
- MongoDB query to replace value with aggregation?
- Match between fields in MongoDB aggregation framework?
- MongoDB aggregation with equality inside array?
- MongoDB aggregation framework match OR is possible?
- Get Absolute value with MongoDB aggregation framework?
- How to match date with MongoDB $match?
- MongoDB aggregation to fetch documents with specific field value?
Advertisements