
- 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 maintain the top count of array elements in MongoDB?
You can use aggregate framework. Let us first create a collection with documents −
> db.topCountArrayDemo.insertOne( ... {"StudentId":101 , "StudentSubject": ["C", "MongoDB"]} ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cc6b3209cb58ca2b005e669") } > db.topCountArrayDemo.insertOne( ... {"StudentId":102 , "StudentSubject": ["C", "Java"]} ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cc6b3219cb58ca2b005e66a") } > db.topCountArrayDemo.insertOne( ... {"StudentId":103 , "StudentSubject": ["C", "MongoDB"]} ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cc6b3229cb58ca2b005e66b") }
Following is the query to display all documents from a collection with the help of find() method −
> db.topCountArrayDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cc6b3209cb58ca2b005e669"), "StudentId" : 101, "StudentSubject" : [ "C", "MongoDB" ] } { "_id" : ObjectId("5cc6b3219cb58ca2b005e66a"), "StudentId" : 102, "StudentSubject" : [ "C", "Java" ] } { "_id" : ObjectId("5cc6b3229cb58ca2b005e66b"), "StudentId" : 103, "StudentSubject" : [ "C", "MongoDB" ] }
Following is the query to maintain the top count of array elements in MongoDB −
> db.topCountArrayDemo.aggregate( ... [ ... { ... $unwind: "$StudentSubject" ... }, ... { ... $group: { ... _id: "$StudentSubject", ... Frequency: {$sum: 1} ... } ... }, ... { ... $sort: {Frequency:-1} ... }, ... { ... $limit: 2 ... } ... ] ... ).pretty();
This will produce the following output −
{ "_id" : "C", "Frequency" : 3 } { "_id" : "MongoDB", "Frequency" : 2 }
- Related Articles
- Count number of elements in an array with MongoDB?
- How to filter array elements in MongoDB?
- Get count of array elements from a specific field in MongoDB documents?
- How to count items in array with MongoDB?
- How to count unique elements in the array using java?
- How do I add a value to the top of an array in MongoDB?
- Swift Program to Count the elements of an Array
- MongoDB query to count the size of an array distinctly?
- Count the number of items in an array in MongoDB?
- MongoDB query to count the frequency of every element of the array
- MongoDB query to change order of array elements?
- Using count equivalent in MongoDB to find top users with max occurrence
- Count array elements that divide the sum of all other elements in C++
- How to add together a subset of elements of an array in MongoDB aggregation?
- Move different elements to another array in MongoDB?

Advertisements