
- 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 count number of distinct values per field/ key in MongoDB?
You can use the distinct command for this. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.distinctCountValuesDemo.insertOne({"StudentFirstName":"John","StudentFavouriteSubject":["C","C++","Java","MySQL","C","C++"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a39f193b406bd3df60e07") } > db.distinctCountValuesDemo.insertOne({"StudentFirstName":"Larry","StudentFavouriteSubject":["MongoDB","SQL Server"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a3a1193b406bd3df60e08") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.distinctCountValuesDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8a39f193b406bd3df60e07"), "StudentFirstName" : "John", "StudentFavouriteSubject" : [ "C", "C++", "Java", "MySQL", "C", "C++" ] } { "_id" : ObjectId("5c8a3a1193b406bd3df60e08"), "StudentFirstName" : "Larry", "StudentFavouriteSubject" : [ "MongoDB", "SQL Server" ] }
Here is the query to find a number of distinct values per field/key −
> db.distinctCountValuesDemo.distinct('StudentFavouriteSubject');
The following is the output −
[ "C", "C++", "Java", "MySQL", "MongoDB", "SQL Server" ]
Here is the query to find the length of the distinct value in the array −
> db.distinctCountValuesDemo.distinct('StudentFavouriteSubject').length;
The following is the output −
6
- Related Articles
- How to get distinct list of sub-document field values in MongoDB?
- Count the number of distinct values in MySQL?
- Find all the non-distinct values of a field in MongoDB?
- How to count distinct values in MySQL?
- Limit number of values in a field with MongoDB?
- Limit number of values in a field using MongoDB?
- Count distinct value in MongoDB?
- How to compare field values in MongoDB?
- Get distinct levels of array field in MongoDB?
- MongoDB aggregate to get the count of field values of corresponding duplicate names?
- MongoDB query to select distinct and count?
- How to return distinct values in MySQL and their count?
- How to get max values for distinct elements in MongoDB
- Get distinct record values in MongoDB?
- Count occurrences of known distinct values in MySQL

Advertisements