w3resource
MongoDB Tutorial

MongoDB: db.collection.group() method

db.collection.group

The db.collection.group() method is used to Groups documents in a collection by the specified keys and performs simple aggregation functions such as computing counts and sums.

Syntax:

db.collection.group({ key, reduce, initial [, keyf] [, cond] [, finalize] })

Parameters:

The db.collection.group() accepts a single document that contains the following:

Name Description Required /
Optional
Type
key The field or fields to the group. Returns a key object for use as the grouping key. Required document
reduce An aggregation function that operates on the documents during the grouping operation. These functions may return a sum or a count. The function takes two arguments: the current document and an aggregation result document for that group. Required function
initial Initializes the aggregation result document. Required initial
keyf Alternative to the key field. Specifies a function that creates a key object for use as the grouping key. Use keyf instead of key to group by calculated fields rather than existing document fields. Optional function
cond The selection criteria to determine which documents in the collection to process. If you omit the cond field, db.collection.group() processes all the documents in the collection for the group operation. Required document
finalize A function that runs each item in the result set before db.collection.group() returns the final value. This function can either modify the result document or replace the result document as a whole. Optional function

Assume that the sample collection is empdetails:


{
        "_id" : ObjectId("56750affdaac17575df77f3e"),
        "emp_code" : "E005",
        "emp_name" : "Alan Hogg",
        "date_of_join" : "15/09/2013",
        "salary" : 19000,
        "gross" : {
                "basic" : 12000,
                "da" : 5000,
                "hra" : 2000
        },
        "deduction" : {
                "pf" : 2000,
                "pt" : 300,
                "it" : 200
        }
}
{
        "_id" : ObjectId("567532f0f61afaaed2aae48c"),
        "emp_code" : "E006",
        "emp_name" : "Karlos Mint",
        "date_of_join" : "23/05/2010",
        "salary" : 17000,
        "gross" : {
                "basic" : 11000,
                "da" : 4500,
                "hra" : 1500
        },
        "deduction" : {
                "pf" : 3000,
                "pt" : 300,
                "it" : 400
        }
}
{
        "_id" : ObjectId("567532fbf61afaaed2aae48d"),
        "emp_code" : "E007",
        "emp_name" : "Burg Hence",
        "date_of_join" : "27/08/2011",
        "salary" : 20000,
        "gross" : {
                "basic" : 14000,
                "da" : 5000,
                "hra" : 1000
        },
        "deduction" : {
                "pf" : 2500,
                "pt" : 300,
                "it" : 200
        }
}
{
        "_id" : ObjectId("56753307f61afaaed2aae48e"),
        "emp_code" : "E004",
        "emp_name" : "Kim Nail",
        "date_of_join" : "16/10/2010",
        "salary" : 17000,
        "gross" : {
                "basic" : 14000,
                "da" : 3000,
                "hra" : 0
        },
        "deduction" : {
                "pf" : 2000,
                "pt" : 300,
                "it" : 200
        }
}

Example: Group by Two Fields

The following example groups by the date_of_join and gross.basic fields those documents that have date_of_join greater than 01/01/2009.


db.empdetails.group(
   {
     key: { "date_of_join": 1, "gross.basic": 1 },
     cond: { "date_of_join": { $gt: "01/01/2009" } },
     reduce: function ( curr, result ) { },
     initial: { }
   }
);

Output

[
        {
                "date_of_join" : "15/09/2013",
                "gross.basic" : 12000
        },
        {
                "date_of_join" : "23/05/2010",
                "gross.basic" : 11000
        },
        {
                "date_of_join" : "27/08/2011",
                "gross.basic" : 14000
        },
        {
                "date_of_join" : "16/10/2010",
                "gross.basic" : 14000
        }
]

Example: Group by Two Fields

The following example groups by the date_of_join and gross.basic fields, those documents that have date_of_join greater than 01/01/2009 and calculates the sum of the basic field for each grouping:

db.empdetails.group(
   {
     key: { "date_of_join": 1, "gross.basic": 1 },
     cond: { "date_of_join": { $gt: "01/01/2009" } },
     reduce: function ( curr, result ) {
                 result.total += curr.gross.basic;
             },
     initial: { total : 0 }
   }
);

Output:

[
        {
                "date_of_join" : "15/09/2013",
                "gross.basic" : 12000,
                "total" : 12000
        },
        {
                "date_of_join" : "23/05/2010",
                "gross.basic" : 11000,
                "total" : 11000
        },
        {
                "date_of_join" : "27/08/2011",
                "gross.basic" : 14000,
                "total" : 14000
        },
        {
                "date_of_join" : "16/10/2010",
                "gross.basic" : 14000,
                "total" : 14000
        }
]

Retrieve the restaurants data from here

Previous: db.collection. getShardVersion() method
Next: db.collection.insert() method



New Content: Composer: Dependency manager for PHP, R Programming