
- 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
GROUP BY array of document to get the count of repeated Age values
To GROUP BY array of the document, use $group. Let us create a collection with documents −
>db.demo559.insertOne({details:[{Name:"Chris",Age:21},{Name:"Bob",Age:22},{Name:"Carol", Age:21},{Name:"Sam",Age:21}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e8f38d954b4472ed3e8e866") }
Display all documents from a collection with the help of find() method −
> db.demo559.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e8f38d954b4472ed3e8e866"), "details" : [ { "Name" : "Chris", "Age" : 21 }, { "Name" : "Bob", "Age" : 22 }, { "Name" : "Carol", "Age" : 21 }, { "Name" : "Sam", "Age" : 21 } ] }
Following is the query to group by an array of the document −
> db.demo559.aggregate([ ... { ... "$unwind": "$details" ... }, ... { ... "$group": { ... "_id": "$details.Age", ... "Count": { "$sum" : 1 } ... } ... }, ... { "$sort": { "_id" : 1 } } ... ])
This will produce the following output −
{ "_id" : 21, "Count" : 3 } { "_id" : 22, "Count" : 1 }
- Related Articles
- MongoDB Group query to get the count of repeated marks in documents?
- Can I get the count of repeated values in a column with MySQL?
- Get the aggregated result and find the count of repeated values in different MongoDB\ndocuments
- MySQL query to group results by date and display the count of duplicate values?
- Group array by equal values JavaScript
- How to get the maximum count of repeated letters in a string? JavaScript
- How to count number of occurrences of repeated names in an array - JavaScript?
- Group with multiple fields and get the count of duplicate field values grouped together in\nMongoDB
- MySQL Query to get count of unique values?
- How to group by age in pivot table?
- Group values in array by two properties JavaScript
- How to get distinct list of sub-document field values in MongoDB?
- MongoDB Aggregate to get average from document and of array elements?
- MongoDB aggregate to get the count of field values of corresponding duplicate names?
- Count of only repeated element in a sorted array of consecutive elements in C++

Advertisements