- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 aggregation with equality inside array?
For this, use aggregate() along with $group. Let us create a collection with documents −
> db.demo578.insertOne( ... { ... "_id" : 1, ... "Info" : { ... "firstName" : "Chris", ... "lastName" : "Brown" ... }, ... ... "achievements" : [ ... { ... "winner" : "10th", ... "year" : 2010, ... "by" : "School" ... }, ... { ... "winner" : "12th", ... "year" : 2012, ... "by" : "School" ... }, ... { ... "winner" : "Good Rank", ... "year" : 2012, ... "by" : "College" ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : 1 }
Display all documents from a collection with the help of find() method −
> db.demo578.find();
This will produce the following output −
{ "_id" : 1, "Info" : { "firstName" : "Chris", "lastName" : "Brown" }, "achievements" : [ { "winner" : "10th", "year" : 2010, "by" : "School" }, { "winner" : "12th", "year" : 2012, "by" : "School" }, { "winner" : "Good Rank", "year" : 2012, "by" : "College" } ] }
Following is the query for aggregation with equality inside an array −
> db.demo578.aggregate([ ... { ... "$unwind": "$achievements" ... }, ... { ... "$group": { ... "_id": { ... "year": "$achievements.year", ... "firstName": "$Info.firstName", ... "lastName": "$Info.lastName" ... }, ... "count": { "$sum": 1 }, ... "AchievementName": { "$push": "$Info" } ... } ... }, ... { ... "$match": { "count": 2 } ... }, ... { ... "$project": { ... "_id": 0, ... "year": "$_id.year", ... "AchievementName": 1, ... "count": 1 ... } ... } ... ]).pretty();
This will produce the following output −
{ "count" : 2, "AchievementName" : [ { "firstName" : "Chris", "lastName" : "Brown" }, { "firstName" : "Chris", "lastName" : "Brown" } ], "year" : 2012 }
- Related Articles
- MongoDB Aggregation to slice array inside array
- Apply a condition inside subset in MongoDB Aggregation?
- MongoDB aggregation with multiple keys
- Match multiple criteria inside an array with MongoDB?
- Perform min/max with MongoDB aggregation
- Coalesce values from different properties into a single array with MongoDB aggregation
- MongoDB aggregation group and remove duplicate array values?
- How to match multiple criteria inside an array with MongoDB?
- Get Absolute value with MongoDB aggregation framework?
- How to use $ifNull with MongoDB aggregation?
- MongoDB query to replace value with aggregation?
- MongoDB aggregation framework with group query example?
- Count by multiple fields with MongoDB aggregation
- How to group nested fields in MongoDB aggregation with count value in array?
- MongoDB Increment value inside nested array?

Advertisements