
- 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
Perform group and distinct together in a single MongoDB query
For this, simply use MongoDB $group. Let us first create a collection with documents −
> db.demo16.insertOne({ ... "StudentName" : "Chris", ... "StudentSection" : "A", ... "StudentAge" : 23, ... "StudentMarks" : 47 ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e13827455d0fc6657d21f07") } > db.demo16.insertOne({ ... "StudentName" : "Bob", ... "StudentSection" : "B", ... "StudentAge" : 21, ... "StudentMarks" : 85 ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e13827555d0fc6657d21f08") } > db.demo16.insertOne( { ... "StudentName" : "Carol", ... "StudentSection" : "A", ... "StudentAge" : 26, ... "StudentMarks" : 97 ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e13827655d0fc6657d21f09") }
Following is the query to display all documents from a collection with the help of find() method −
> db.demo16.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e13827455d0fc6657d21f07"), "StudentName" : "Chris", "StudentSection" : "A", "StudentAge" : 23, "StudentMarks" : 47 } { "_id" : ObjectId("5e13827555d0fc6657d21f08"), "StudentName" : "Bob", "StudentSection" : "B", "StudentAge" : 21, "StudentMarks" : 85 } { "_id" : ObjectId("5e13827655d0fc6657d21f09"), "StudentName" : "Carol", "StudentSection" : "A", "StudentAge" : 26, "StudentMarks" : 97 }
Following is the query to implement group and distinct operation together −
> db.demo16.aggregate([{ ... $group : { ... _id : null, ... StudentName : { $addToSet : "$StudentName" }, ... StudentSection : { $addToSet : "$StudentSection" }, ... StudentMinimumAge : { $min : "$StudentAge" }, ... StudentMaximumAge : { $max : "$StudentAge" }, ... StudentMinimumMarks: { $min : "$StudentMarks" }, ... StudentMaximumMarks : { $max : "$StudentMarks" } ... } ... }]).pretty();
This will produce the following output −
{ "_id" : null, "StudentName" : [ "Carol", "Bob", "Chris" ], "StudentSection" : [ "B", "A" ], "StudentMinimumAge" : 21, "StudentMaximumAge" : 26, "StudentMinimumMarks" : 47, "StudentMaximumMarks" : 97 }
- Related Articles
- Using DISTINCT and COUNT together in a MySQL Query?
- A single MongoDB query to orderby date and group by user
- MySQL query to sort multiple columns together in a single query
- MongoDB query to select distinct and count?
- Counting different distinct items in a single MySQL query?
- MongoDB query for a single field
- Sort and Group in one MongoDB aggregation query?
- Update multiple rows in a single MongoDB query?
- MySQL query to group concat distinct by Id?
- Perform Group in MongoDB and sum the price record of documents
- How to efficiently perform “distinct” with multiple keys in MongoDB?
- MongoDB query to get only distinct values
- Group query upon nested object in MongoDB?
- MongoDB query to push a computed expression in a $group?
- Perform SUM and SUBTRACTION on the basis of a condition in a single MySQL query?

Advertisements