
- 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
Get the aggregated result and find the count of repeated values in different MongoDB
documents
To get the count of repeated values in different documents, use aggregate(). Let us create a collection with documents −
> db.demo452.insertOne({"StudentName":"John","StudentAge":21});{ "acknowledged" : true, "insertedId" : ObjectId("5e7b7e3371f552a0ebb0a6f3") } > db.demo452.insertOne({"StudentName":"John","StudentAge":22});{ "acknowledged" : true, "insertedId" : ObjectId("5e7b7e3671f552a0ebb0a6f4") } > db.demo452.insertOne({"StudentName":"John","StudentAge":23});{ "acknowledged" : true, "insertedId" : ObjectId("5e7b7e3971f552a0ebb0a6f5") } > db.demo452.insertOne({"StudentName":"David","StudentAge":24});{ "acknowledged" : true, "insertedId" : ObjectId("5e7b7e4371f552a0ebb0a6f6") } > db.demo452.insertOne({"StudentName":"David","StudentAge":25});{ "acknowledged" : true, "insertedId" : ObjectId("5e7b7e4571f552a0ebb0a6f7") }
Display all documents from a collection with the help of find() method −
> db.demo452.find();
This will produce the following output −
{ "_id" : ObjectId("5e7b7e3371f552a0ebb0a6f3"), "StudentName" : "John", "StudentAge" : 21 } { "_id" : ObjectId("5e7b7e3671f552a0ebb0a6f4"), "StudentName" : "John", "StudentAge" : 22 } { "_id" : ObjectId("5e7b7e3971f552a0ebb0a6f5"), "StudentName" : "John", "StudentAge" : 23 } { "_id" : ObjectId("5e7b7e4371f552a0ebb0a6f6"), "StudentName" : "David", "StudentAge" : 24} { "_id" : ObjectId("5e7b7e4571f552a0ebb0a6f7"), "StudentName" : "David", "StudentAge" : 25}
Following is the query to find the count of repeated values in different MongoDB documents −
> db.demo452.aggregate([ ... {$group: {_id:"$StudentName", count:{$sum:1}}}, ... {$sort: {count:-1}}, ... ... {$group: {_id:1, StudentName:{$push:{StudentName:"$_id", count:"$count"}}}}, ... {$project: { ... first : {$arrayElemAt: ["$StudentName", 0]}, ... second: {$arrayElemAt: ["$StudentName", 1]}, ... others: {$slice:["$StudentName", 2, {$size: "$StudentName"}]} ... } ... }, ... ... {$project: { ... status: [ ... "$first", ... "$second", ... { ... StudentName: "New Student Name", ... count: {$sum: "$others.count"} ... } ... ] ... } ... }, ... ... {$unwind: "$status"}, ... {$project: { _id:0, StudentName: "$status.StudentName", count: "$status.count" }} ... ])
This will produce the following output −
{ "StudentName" : "John", "count" : 3 } { "StudentName" : "David", "count" : 2 } { "StudentName" : "New Student Name", "count" : 0 }
- Related Articles
- MongoDB Group query to get the count of repeated marks in documents?
- Find in MongoDB documents with filled nested array and reshape the documents result
- Get the count of the number of documents in a MongoDB Collection?
- Can I get the count of repeated values in a column with MySQL?
- GROUP BY array of document to get the count of repeated Age values
- Count values based on conditions and display the result in different columns with MySQL?
- Find values with OR operator in MongoDB and format the result.?
- Get count of array elements from a specific field in MongoDB documents?
- MongoDB aggregate to get the Mean daily average count of recorded documents in a collection?
- Get a count of total documents with MongoDB while using limit?
- MongoDB query to get distinct FirstName values from documents
- Sum the score of duplicate column values in MongoDB documents?
- Aggregate based on array value to sum values in different MongoDB documents?
- How to count the number of documents in a MongoDB collection?
- Get the size of all the documents in a MongoDB query?

Advertisements