
- 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
Count distinct value in MongoDB?
Use the concept of length to count distinct value. Following is the syntax −
db.yourCollectionName.distinct("yourFieldName").length;
Let us create a collection with documents −
> db.countDistinctDemo.insertOne({"StudentName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5cbd6166de8cc557214c0dfa") } > db.countDistinctDemo.insertOne({"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5cbd616ade8cc557214c0dfb") } > db.countDistinctDemo.insertOne({"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5cbd616cde8cc557214c0dfc") } > db.countDistinctDemo.insertOne({"StudentName":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5cbd6170de8cc557214c0dfd") } > db.countDistinctDemo.insertOne({"StudentName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5cbd6175de8cc557214c0dfe") } > db.countDistinctDemo.insertOne({"StudentName":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5cbd6181de8cc557214c0dff") }
Display all documents from a collection with the help of find() method −
> db.countDistinctDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cbd6166de8cc557214c0dfa"), "StudentName" : "John" } { "_id" : ObjectId("5cbd616ade8cc557214c0dfb"), "StudentName" : "Chris" } { "_id" : ObjectId("5cbd616cde8cc557214c0dfc"), "StudentName" : "Chris" } { "_id" : ObjectId("5cbd6170de8cc557214c0dfd"), "StudentName" : "Carol" } { "_id" : ObjectId("5cbd6175de8cc557214c0dfe"), "StudentName" : "David" } { "_id" : ObjectId("5cbd6181de8cc557214c0dff"), "StudentName" : "Carol" }
Following is the query to count distinct value −
> db.countDistinctDemo.distinct("StudentName").length;
This will produce the following output −
4
- Related Articles
- MongoDB query to select distinct and count?
- How to count number of distinct values per field/ key in MongoDB?
- Get the count of a specific value in MongoDB
- How to get the count of each distinct value in a column in MySQL?
- Count distinct entries in SAP BusinessObjects
- Get the count of a specific value in MongoDB quickly
- Get distinct record values in MongoDB?
- MySQL SELECT DISTINCT and count?
- How to count distinct values in MySQL?
- Absolute distinct count in a sorted array?
- Performing distinct on multiple fields in MongoDB?
- Count distinct elements in an array in Python
- Count distinct elements in an array in C++
- How to count the distinct column in MySQL?
- Get distinct values and count them in MySQL

Advertisements