
- 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
Performing distinct on multiple fields in MongoDB?
You can use $group operator along with aggregate framework to perform distinct on multiple fields. Let us first create a collection with documents −
> db.distinctOnMultipleFieldsDemo.insertOne( ... { ... "StudentFirstName" : "Chris", ... "StudentAge" : 21, ... "StudentCountryName": "US" ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd2e518b64f4b851c3a13d7") } > db.distinctOnMultipleFieldsDemo.insertOne( ... { ... "StudentFirstName" : "Robert", ... "StudentAge" : 21, ... "StudentCountryName": "AUS" ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd2e518b64f4b851c3a13d8") } > db.distinctOnMultipleFieldsDemo.insertOne( ... { ... "StudentFirstName" : "Chris", ... "StudentAge" : 22, ... "StudentCountryName": "UK" ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd2e518b64f4b851c3a13d9") } > db.distinctOnMultipleFieldsDemo.insertOne( ... { ... "StudentFirstName" : "David", ... "StudentAge" : 24, ... "StudentCountryName": "AUS" ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd2e518b64f4b851c3a13da") }
Following is the query to display all documents from a collection with the help of find() method −
> db.distinctOnMultipleFieldsDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd2e518b64f4b851c3a13d7"), "StudentFirstName" : "Chris", "StudentAge" : 21, "StudentCountryName" : "US" } { "_id" : ObjectId("5cd2e518b64f4b851c3a13d8"), "StudentFirstName" : "Robert", "StudentAge" : 21, "StudentCountryName" : "AUS" } { "_id" : ObjectId("5cd2e518b64f4b851c3a13d9"), "StudentFirstName" : "Chris", "StudentAge" : 22, "StudentCountryName" : "UK" } { "_id" : ObjectId("5cd2e518b64f4b851c3a13da"), "StudentFirstName" : "David", "StudentAge" : 24, "StudentCountryName" : "AUS" }
Following is the query to perform distinct on multiple fields in MongoDB −
> db.distinctOnMultipleFieldsDemo.aggregate([ ... { ... $group: ... { ... _id:0, ... StudentFirstName: {$addToSet: '$StudentFirstName'}, ... StudentAge: {$addToSet: '$StudentAge'}, ... StudentCountryName: {$addToSet: '$StudentCountryName'}, ... }} ... ]);
This will produce the following output −
{ "_id" : 0, "StudentFirstName" : [ "David", "Robert", "Chris" ], "StudentAge" : [ 24, 22, 21 ], "StudentCountryName" : [ "UK", "AUS", "US" ] }
- Related Articles
- Search multiple fields for multiple values in MongoDB?
- MongoDB query for exact match on multiple document fields
- How do you compare two fields in MongoDB while performing an operation on one of them?
- Count by multiple fields with MongoDB aggregation
- Prevent duplicates of multiple fields with index in MongoDB
- How to find MongoDB documents in a collection with a filter on multiple combined fields?
- How to efficiently perform “distinct” with multiple keys in MongoDB?
- MongoDB query to check the existence of multiple fields
- How do I index “or” in MongoDB for indexing multiple fields?
- Select two fields and return a sorted array with their distinct values in MongoDB?
- Get fields from multiple sub-documents that match a condition in MongoDB?
- MongoDB query condition on comparing 2 fields?
- Performing multiple transitions using CSS3
- Concatenate fields in MongoDB?
- How to use MySQL DISTINCT clause on multiple columns?

Advertisements