- 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 query for counting number of unique fields grouped by another field?
For this, use aggregate() and group with $group. Let us create a collection with documents −
> db.demo354.insertOne({"Name1":"Chris","Name2":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e5685a6f8647eb59e5620c0") } > db.demo354.insertOne({"Name1":"Chris","Name2":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e5685a9f8647eb59e5620c1") } > db.demo354.insertOne({"Name1":"Chris","Name2":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e5685aff8647eb59e5620c2") } > db.demo354.insertOne({"Name1":"Carol","Name2":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5e5685c4f8647eb59e5620c3") } > db.demo354.insertOne({"Name1":"Carol","Name2":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5e5685c5f8647eb59e5620c4") }
Display all documents from a collection with the help of find() method −
> db.demo354.find();
This will produce the following output −
{ "_id" : ObjectId("5e5685a6f8647eb59e5620c0"), "Name1" : "Chris", "Name2" : "David" } { "_id" : ObjectId("5e5685a9f8647eb59e5620c1"), "Name1" : "Chris", "Name2" : "David" } { "_id" : ObjectId("5e5685aff8647eb59e5620c2"), "Name1" : "Chris", "Name2" : "Bob" } { "_id" : ObjectId("5e5685c4f8647eb59e5620c3"), "Name1" : "Carol", "Name2" : "John" } { "_id" : ObjectId("5e5685c5f8647eb59e5620c4"), "Name1" : "Carol", "Name2" : "John" }
Following is the query for counting number of unique fields grouped by another field −
> db.demo354.aggregate([ ... { ... $group: { ... _id: { ... "Name1": "$Name1", ... "Name2": "$Name2" ... }, ... } ... }, ... { ... $group: { ... _id: "$_id.Name1", ... "Name2": { $sum: 1 } ... } ... }, ... { ... $project: { ... _id: 0, ... Name1: "$_id", ... Name2: 1 ... } ... } ... ])
This will produce the following output −
{ "Name2" : 2, "Name1" : "Chris" } { "Name2" : 1, "Name1" : "Carol" }
- Related Articles
- Select documents grouped by field in MongoDB?
- MongoDB query by sub-field?
- MongoDB query to update array with another field?
- MongoDB query for fields in embedded document?
- MongoDB query for a single field
- MongoDB query for counting the distinct values across all documents?
- MongoDB query for exact match on multiple document fields
- Find values group by another field in MongoDB?
- How to ORDER BY grouped fields in MySQL?
- MongoDB query to create new field and count set the count of another field in it?
- Update MongoDB field using value of another field?
- MongoDB Query for boolean field as “not true”
- MongoDB query to update selected fields
- MongoDB query to sum specific fields
- Fetch max date and other field from the same row of a table grouped by other fields in SAP HANA

Advertisements