
- 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 query to implement aggregate function
Let us first create a collection with documents −
> db.demo121.insertOne( ... { ... "Id" : 101, ... "Details" : [ ... { ... "SubjectId" : "1", ... "SubjectName" : "MongoDB", ... "Score" : 76 ... }, ... { ... "SubjectId" : "2", ... "SubjectName" : "MySQL", ... "Score" : 76 ... }, ... { ... "SubjectId" : "3", ... "SubjectName" : "Java", ... "Score" : 76 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2f1c60140daf4c2a3544b3") }
Display all documents from a collection with the help of find() method −
> db.demo121.find();
This will produce the following output −
{ "_id" : ObjectId("5e2f1c60140daf4c2a3544b3"), "Id" : 101, "Details" : [ { "SubjectId" : "1", "SubjectName" : "MongoDB", "Score" : 76 }, { "SubjectId" : "2", "SubjectName" : "MySQL", "Score" : 76 }, { "SubjectId" : "3", "SubjectName" : "Java", "Score" : 76 } ] }
Following is the query to implement aggregate function −
> db.demo121.aggregate([ ... { "$match": { "Id": 101 } }, ... { "$unwind": "$Details" }, ... { ... "$group": { ... "_id": "$Details.SubjectId", ... "count": { "$sum": 1 }, ... "Details": { ... "$push": { ... "SubjectName": "$Details.SubjectName" ... } ... } ... } ... }, ... { ... "$group": { ... "_id": null, ... "List": { ... "$push": { ... "SubId": "$_id", ... "Details": "$Details" ... } ... } ... } ... } ... ], function (err, out) { ... res.json(out); ... });
This will produce the following output −
{ "_id" : null, "List" : [ { "SubId" : "3", "Details" : [ { "SubjectName" : "Java" } ] }, { "SubId" : "2", "Details" : [ { "SubjectName" : "MySQL" } ] }, { "SubId" : "1", "Details" : [ { "SubjectName" : "MongoDB" } ] } ] }
- Related Articles
- MongoDB aggregate query to sort
- MongoDB query to aggregate nested array
- Implement MongoDB Aggregate - unwind, group and project?
- Implement $match and $project in MongoDB aggregate
- How to implement MongoDB $or query?
- MongoDB Query to implement $in in array
- MongoDB query to implement OR operator in find()
- MongoDB query to execute stored function?
- Implement a query similar to MySQL Union with MongoDB?
- MongoDB query to implement nor query to fetch documents except a specific document
- How to use MongoDB Aggregate to sort?
- How to aggregate array documents in MongoDB?
- How to update after aggregate in MongoDB?
- Invert Result of MongoDB Query (Implement Opposite of $and operation)?
- SQL query describing usage of SUM aggregate function and GROUP-BY with HAVING

Advertisements