
- 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
MongoDB aggregation to combine or merge fields and then count?
To combine or merge fields and then perform count, use $group along with $sum and $sort. Let us create a collection with documents −
> db.demo647.insertOne({"Subject":"MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5e9c86316c954c74be91e6ee") } > db.demo647.insertOne({"Subject":"MongoDB"}); { "acknowledged" : true, "insertedId" : ObjectId("5e9c86356c954c74be91e6ef") } > db.demo647.insertOne({"Subject":"MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5e9c86376c954c74be91e6f0") } > db.demo647.insertOne({"Subject":"SQL Server"}); { "acknowledged" : true, "insertedId" : ObjectId("5e9c86406c954c74be91e6f1") } > db.demo647.insertOne({"Subject":"MongoDB"}); { "acknowledged" : true, "insertedId" : ObjectId("5e9c86436c954c74be91e6f2") } > db.demo647.insertOne({"Subject":"PL/SQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5e9c864b6c954c74be91e6f3") } > db.demo647.insertOne({"Subject":"MongoDB"}); { "acknowledged" : true, "insertedId" : ObjectId("5e9c86c16c954c74be91e6f4") }
Display all documents from a collection with the help of find() method −
> db.demo647.find();
This will produce the following output −
{ "_id" : ObjectId("5e9c86316c954c74be91e6ee"), "Subject" : "MySQL" } { "_id" : ObjectId("5e9c86356c954c74be91e6ef"), "Subject" : "MongoDB" } { "_id" : ObjectId("5e9c86376c954c74be91e6f0"), "Subject" : "MySQL" } { "_id" : ObjectId("5e9c86406c954c74be91e6f1"), "Subject" : "SQL Server" } { "_id" : ObjectId("5e9c86436c954c74be91e6f2"), "Subject" : "MongoDB" } { "_id" : ObjectId("5e9c864b6c954c74be91e6f3"), "Subject" : "PL/SQL" } { "_id" : ObjectId("5e9c86c16c954c74be91e6f4"), "Subject" : "MongoDB" }
Following is the query to combine or merge fields then count −
> db.demo647.aggregate([ { "$group": { "_id": "$Subject", "COUNT": { "$sum": 1 } } }, { "$sort": { "COUNT": -1 } }, { "$limit": 2 } ] );
This will produce the following output −
{ "_id" : "MongoDB", "COUNT" : 3 } { "_id" : "MySQL", "COUNT" : 2 }
- Related Articles
- Count by multiple fields with MongoDB aggregation
- How to group nested fields in MongoDB aggregation with count value in array?
- MongoDB Query to combine AND & OR?
- Match between fields in MongoDB aggregation framework?
- Merge two array fields in MongoDB?
- MongoDB query to group several fields using aggregation framework?
- How to compare two fields in aggregation filter with MongoDB?
- Is it possible to sum two fields in MongoDB using the Aggregation framework?
- MongoDB aggregation framework match OR is possible?
- MongoDB aggregation and projection?
- How to exclude _id without including other fields using the aggregation framework in MongoDB?
- MongoDB divide aggregation operator?
- How do I use MongoDB to count only collections that match two fields?
- Include all existing fields and add new fields to document in MongoDB?
- Count unique items in array-based fields across all MongoDB documents?

Advertisements