- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get MongoDB documents with max attribute per group in a collection?
You can get documents with max attribute per group in a collection using $sort operator along with $group statement.
To understand the concept further, let us create a collection with document. The query to create a collection with document is as follows −
> db.maxAttributePerGroup.insertOne({"StudentFirstName":"John","StudentLastName":"Smith ","StudentAge":29,"StudentId":10}); { "acknowledged" : true, "insertedId" : ObjectId("5c76ee341e9c5dd6f1f78277") } > db.maxAttributePerGroup.insertOne({"StudentFirstName":"Carol","StudentLastName":"Taylo r","StudentAge":19,"StudentId":10}); { "acknowledged" : true, "insertedId" : ObjectId("5c76ee4e1e9c5dd6f1f78278") } > db.maxAttributePerGroup.insertOne({"StudentFirstName":"Adam","StudentLastName":"Smit h","StudentAge":34,"StudentId":20}); { "acknowledged" : true, "insertedId" : ObjectId("5c76ee631e9c5dd6f1f78279") } > db.maxAttributePerGroup.insertOne({"StudentFirstName":"Bob","StudentLastName":"Taylor" ,"StudentAge":58,"StudentId":20}); { "acknowledged" : true, "insertedId" : ObjectId("5c76ee791e9c5dd6f1f7827a") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.maxAttributePerGroup.find().pretty();
Output
{ "_id" : ObjectId("5c76ee341e9c5dd6f1f78277"), "StudentFirstName" : "John", "StudentLastName" : "Smith", "StudentAge" : 29, "StudentId" : 10 } { "_id" : ObjectId("5c76ee4e1e9c5dd6f1f78278"), "StudentFirstName" : "Carol", "StudentLastName" : "Taylor", "StudentAge" : 19, "StudentId" : 10 } { "_id" : ObjectId("5c76ee631e9c5dd6f1f78279"), "StudentFirstName" : "Adam", "StudentLastName" : "Smith", "StudentAge" : 34, "StudentId" : 20 } { "_id" : ObjectId("5c76ee791e9c5dd6f1f7827a"), "StudentFirstName" : "Bob", "StudentLastName" : "Taylor", "StudentAge" : 58, "StudentId" : 20 }
Here is the query to get documents with max attribute per group in a collection −
> db.maxAttributePerGroup.aggregate([ ... {"$sort":{"StudentId":1,"StudentAge":-1}}, ... {$group:{ ... "_id":"$StudentId", ... "StudentAge":{"$first":"$StudentAge"}, ... "StudentFirstName":{"$first":"$StudentFirstName"}, ... "StudentLastName":{"$first":"$StudentLastName"} ... }} ]).pretty();
Output
{ "_id" : 20, "StudentAge" : 58, "StudentFirstName" : "Bob", "StudentLastName" : "Taylor" } { "_id" : 10, "StudentAge" : 29, "StudentFirstName" : "John", "StudentLastName" : "Smith" }
- Related Articles
- Get all embedded documents with “isMarried” status in a MongoDB collection
- Get the maximum mark records from a collection with documents in MongoDB
- Get the maximum mark records from a collection with documents in MongoDB query
- Get the count of the number of documents in a MongoDB Collection?
- Find a value in lowercase from a MongoDB collection with documents
- Group all documents with common fields in MongoDB?
- Evaluate one of more values from a MongoDB collection with documents
- Group by dates in a MongoDB collection?
- Set max on MongoDB capped collection?
- How to delete documents from a collection in MongoDB?
- How to retrieve documents from a collection in MongoDB?
- MongoDB Group query to get the count of repeated marks in documents?
- MongoDB aggregate to get the Mean daily average count of recorded documents in a collection?
- Aggregate by country, state and city in a MongoDB collection with multiple documents
- MongoDB query to update each field of documents in collection with a formula?

Advertisements