Skip to main content

$group (aggregation) in MongoDB

IF we want to group document by specific expression and want to output for each distinct grouping of document here we have to use $group. IF you familiar with Relational Database like SQL Server, It's work same like GROUP BY clause.

Output document contains the _id field which contains the distinct group by key also output document contains computed fields which grasp the value of some accumulator expression grouped by the _id.

Syntax:


{ $group: { _id: <expression>, <field1>: { <accumulator1> : <expression1> }, ... }


Accumulator Operator:

$num : It's returns sum of the numeric value. It can be use in $project also in mongodb 3.2 version.

$avg  : It's returns the average of numeric values. It can be use in $project also in mongodb 3.2 version.

$first : It's returns a value from the first document for each group.

$last : It's returns a value from the first document for each group.

$max : It's returns the highest expression value for each group. It can be use in $project also in mongodb 3.2 version.

$min : It's returns the lowest expression value for each group.  It can be use in $project also in mongodb 3.2 version.

$push : It's returns the an array of expression values for each group.

$addToSet : It's returns an array of unique expression values for each group. Order of the array elements is undefined.

$stdDevPop : It's returns the population standard deviation of the input values.

$stdDevSamp : It's returns the sample standard deviation of the input values.

Example :


{ "_id" : 1, "Book Title": "book1", "price" : 20, "quantity" : 1, "date" : ISODate("2016-08-05T07:00:00Z") }
{ "_id" : 2, " Book Title " : " book2", "price" : 10, "quantity" : 2, "date" : ISODate("2016-08-05T08:00:00Z") }
{ "_id" : 3, " Book Title " : " book3", "price" : 30, "quantity" : 4, "date" : ISODate("2016-08-17T10:00:00Z") }
{ "_id" : 4, " Book Title " : " book4", "price" : 10, "quantity" : 2, "date" : ISODate("2014-09-01T11:20:39.736Z") }
{ "_id" : 5, " Book Title " : " book5", "price" : 20, "quantity" : 6, "date" : ISODate("2014-09-04T20:23:13.331Z") }


Group by Month, and Year


db.BookOrder.aggregate(
   [
      {
        $group : {
           _id : { month: { $month: "$date" , year: { $year: "$date" } },
           totalAmount: { $sum: { $multiply: [ "$price", "$quantity" ] } },
           totalQty: { $sum: "$quantity" },
           count: { $sum: 1 }
        }
      }
   ]
)


Result:


{ "_id" : { "month" : 8, "year" : 2016 }, " totalAmount " : 60, " totalQty " : 7, "count" : 3 }
{ "_id" : { "month" : 9, "year" : 2016 }, " totalAmount " : 30, " totalQty " : 8, "count" : 2 }


Note : _id filed is mandatory, except _id all fields is optional and computed using accumulator.

Limitation of Group Stage: $group stage memory can't exceed more than 100 megabytes RAM. If $group stage exceed his limit by default give an error.

Popular posts from this blog

Check for changes to an SQL Server table?

Problem: Suppose your team is working on the under-development project so it might be possible continuous work on the database and perform changes in Table, Stored procedure as per requirement, and daily you have to update the testing server database as per changes are done in developing server database then how it is possible to trace those changes. There are a lot of solutions for this problem which is listed below Solution 1: For SQL Server 2000, 2005 and above use the CHECKSUM command SELECTCHECKSUM_AGG(BINARY_CHECKSUM(*))FROMYour_Table_NameWITH (NOLOCK); That will return the same number each time its run as long as the table contents haven't changed. Unfortunately CHECKSUM does not work always properly to detect changes. It is only a primitive checksum and no CRC calculation. Therefore you can't use it to detect all changes, e. g. symmetrical changes result in the same CHECKSUM! Solution 2: 1.Run the following query. Before executing query replace DB_Name with your database name…

How to drop multiple tables with common prefix in one query?

Problem: Suppose we have a situation where we need to drop those tables that have some prefixes string, so it is possible to drop those tables with a common prefix in a single query.
Solution: yes it is possible to drop all those tables that have the common prefix in a single query. Using following query you can delete all those tables that begin with a certain prefix string. In where condition just put the common prefix string in where condition (Like ‘prefix%’)
DECLARE@queryNVARCHAR(MAX)=N'';
SELECT@query+=' DROP TABLE ' +QUOTENAME(s.name) +'.'+QUOTENAME(t.name)+';' FROMsys.tablesASt INNERJOINsys.schemasASs ONt.[schema_id]=s.[schema_id] WHEREt.nameLIKE'MX_100%';
EXECsp_executesql@query;
This query may create an issue, if a table has a foreign key relationship, you'll either need to drop them first or arrange the output to drop the tables in a certain order. If you want to monitor exactly what goes on when the query is running then use the following que…

Merge and Merge join transformation in SSIS