
- 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
MongoDB query for counting the distinct values across all documents?
For this, use aggregate() in MongoDB. Let us create a collection with documents −
> db.demo718.insertOne( ... { ... "id":101, ... "details": ... { ... "OtherDetails": ["Chris", "Mike", "Sam"], "GroupName": ["Group-1"], "Info": [] ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5eaae25843417811278f5880") } > db.demo718.insertOne( ... { ... "id":102, ... "details": ... { ... "OtherDetails": ["Chris", "David"], "GroupName": ["Group-1"], "Info": [] ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5eaae25943417811278f5881") }
Display all documents from a collection with the help of find() method −
> db.demo718.find();
This will produce the following output −
{ "_id" : ObjectId("5eaae25843417811278f5880"), "id" : 101, "details" : { "OtherDetails" : [ "Chris", "Mike", "Sam" ], "GroupName" : [ "Group-1" ], "Info" : [ ] } } { "_id" : ObjectId("5eaae25943417811278f5881"), "id" : 102, "details" : { "OtherDetails" : [ "Chris", "David" ], "GroupName" : [ "Group-1" ], "Info" : [ ] } }
Following is the query to count the distinct values across all documents −
> db.demo718.aggregate([ ... { ... $unwind: "$details.GroupName" ... }, ... { ... $match: { ... "details.GroupName": "Group-1" ... } ... }, ... { ... $unwind: "$details.OtherDetails" ... }, ... { ... $group: { ... _id: "$details.GroupName", ... OtherDetailsUnique: { ... $addToSet: "$details.OtherDetails" ... } ... } ... }, ... { ... $project: { ... _id : 0, ... GROUP_NAME: "$_id", ... OtherDetailsUniqueCount: { ... $size: "$OtherDetailsUnique" ... } ... } ... } ... ])
This will produce the following output −
{ "GROUP_NAME" : "Group-1", "OtherDetailsUniqueCount" : 4 }
- Related Articles
- MongoDB query to get distinct FirstName values from documents
- MongoDB query to get only distinct values
- Count unique items in array-based fields across all MongoDB documents?
- MongoDB query to update all documents matching specific IDs
- Get the size of all the documents in a MongoDB query?
- MongoDB - Query embedded documents?
- MongoDB Aggregate sum of values in a list of dictionaries for all documents?
- How to sum the value of a key across all documents in a MongoDB collection?
- MongoDB query to find matching documents given an array with values?
- MongoDB query to skip documents
- Find all the non-distinct values of a field in MongoDB?
- MongoDB inverse of query to return all items except specific documents?
- Counting different distinct items in a single MySQL query?
- MongoDB query to add up the values of a specific field in documents
- MongoDB query for documents matching array, irrespective of elements order

Advertisements