- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MongoDB Group query to get the count of repeated marks in documents?
For group query, use MongoDB $group and get the count with $sum. Let us create a collection with documents −
> db.demo676.insertOne({"Marks":87}); { "acknowledged" : true, "insertedId" : ObjectId("5ea41eed04263e90dac943f2") } > db.demo676.insertOne({"Marks":75}); { "acknowledged" : true, "insertedId" : ObjectId("5ea41ef304263e90dac943f3") } > db.demo676.insertOne({"Marks":87}); { "acknowledged" : true, "insertedId" : ObjectId("5ea41ef404263e90dac943f4") } > db.demo676.insertOne({"Marks":65}); { "acknowledged" : true, "insertedId" : ObjectId("5ea41ef704263e90dac943f5") } > db.demo676.insertOne({"Marks":65}); { "acknowledged" : true, "insertedId" : ObjectId("5ea41ef804263e90dac943f6") }
Display all documents from a collection with the help of find() method −
> db.demo676.find();
This will produce the following output −
{ "_id" : ObjectId("5ea41eed04263e90dac943f2"), "Marks" : 87 } { "_id" : ObjectId("5ea41ef304263e90dac943f3"), "Marks" : 75 } { "_id" : ObjectId("5ea41ef404263e90dac943f4"), "Marks" : 87 } { "_id" : ObjectId("5ea41ef704263e90dac943f5"), "Marks" : 65 } { "_id" : ObjectId("5ea41ef804263e90dac943f6"), "Marks" : 65 }
Following is the query to get the count of repeated marks −
> db.demo676.aggregate( { $group: { ... _id: {Marks: "$Marks" }, ... 'Count': { $sum :1 } ... }})
This will produce the following output −
{ "_id" : { "Marks" : 75 }, "Count" : 1 } { "_id" : { "Marks" : 65 }, "Count" : 2 } { "_id" : { "Marks" : 87 }, "Count" : 2 }
- Related Articles
- MongoDB query to group duplicate documents
- MongoDB aggregation to get two documents with the least marks
- GROUP BY array of document to get the count of repeated Age values
- Get the size of all the documents in a MongoDB query?
- Get the count of the number of documents in a MongoDB Collection?
- MongoDB query to get distinct FirstName values from documents
- MongoDB query to get only specific fields in nested array documents?
- MongoDB query to get documents with multiple conditions set in $or?
- MongoDB query to skip documents
- Get MongoDB documents with max attribute per group in a collection?
- MongoDB aggregate to get the Mean daily average count of recorded documents in a collection?
- MongoDB query to count the number of array items in documents and display in a new field
- Get the aggregated result and find the count of repeated values in different MongoDB\ndocuments
- Get count of array elements from a specific field in MongoDB documents?
- MongoDB - Query embedded documents?

Advertisements