
- 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 unique items in array-based fields across all MongoDB documents?
To count unique items in array-based fields, use $group along with aggregate(). Let us create a collection with documents −
> db.demo493.insertOne({"SubjectName":["MySQL","MongoDB","Java"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e849f97b0f3fa88e22790c4") } > db.demo493.insertOne({"SubjectName":["C++","MongoDB","C"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e849fa4b0f3fa88e22790c5") } > db.demo493.insertOne({"SubjectName":["MySQL","MongoDB","C"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e849fb2b0f3fa88e22790c6") }
Display all documents from a collection with the help of find() method −
> db.demo493.find();
This will produce the following output −
{ "_id" : ObjectId("5e849f97b0f3fa88e22790c4"), "SubjectName" : [ "MySQL", "MongoDB", "Java" ] } { "_id" : ObjectId("5e849fa4b0f3fa88e22790c5"), "SubjectName" : [ "C++", "MongoDB", "C" ] } { "_id" : ObjectId("5e849fb2b0f3fa88e22790c6"), "SubjectName" : [ "MySQL", "MongoDB", "C" ] }
Following is the query to count unique items in array-based fields across all documents −
> db.demo493.aggregate([ ... { $unwind: "$SubjectName" }, ... { $group: { _id: "$SubjectName", Frequency: { $sum : 1 } } } ... ] ... );
This will produce the following output −
{ "_id" : "C++", "Frequency" : 1 } { "_id" : "C", "Frequency" : 2 } { "_id" : "Java", "Frequency" : 1 } { "_id" : "MySQL", "Frequency" : 2 } { "_id" : "MongoDB", "Frequency" : 3 }
- Related Articles
- Group all documents with common fields in MongoDB?
- JavaScript function that should count all unique items in an array
- Filter documents in MongoDB if all keys exist as fields?
- Match MongoDB documents with fields not containing values in array?
- MongoDB query for counting the distinct values across all documents?
- MongoDB query to get only specific fields in nested array documents?
- MongoDB query to count the number of array items in documents and display in a new field
- How to count items in array with MongoDB?
- Querying array of Embedded Documents in MongoDB based on Range?
- How to filter documents based on an array in MongoDB?
- MongoDB inverse of query to return all items except specific documents?
- Sort array in MongoDB query and project all fields?
- Count number of documents from MongoDB collection inside Array?
- Count the number of items in an array in MongoDB?
- MongoDB aggregation to sum individual properties on an object in an array across documents

Advertisements