- Reference >
mongo
Shell Methods >- Collection Methods >
- db.collection.group()
db.collection.group()¶
On this page
Definition¶
-
db.collection.
group
({ key, reduce, initial [, keyf] [, cond] [, finalize] })¶ Note
Because
db.collection.group()
uses JavaScript, it is subject to a number of performance limitations. For most cases the$group
operator in the aggregation pipeline provides a suitable alternative with fewer restrictions.Groups documents in a collection by the specified keys and performs simple aggregation functions such as computing counts and sums. The method is analogous to a
SELECT <...> GROUP BY
statement in SQL. Thegroup()
method returns an array.The
db.collection.group()
accepts a single document that contains the following:Field Type Description key
document The field or fields to group. Returns a “key object” for use as the grouping key. reduce
function 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. initial
document Initializes the aggregation result document. keyf
function Optional. Alternative to the key
field. Specifies a function that creates a “key object” for use as the grouping key. Usekeyf
instead ofkey
to group by calculated fields rather than existing document fields.cond
document 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.finalize
function Optional. 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.The
db.collection.group()
method is a shell wrapper for thegroup
command. However, thedb.collection.group()
method takes thekeyf
field and thereduce
field whereas thegroup
command takes the$keyf
field and the$reduce
field.
Behavior¶
Limits and Restrictions¶
The db.collection.group()
method does not work with
sharded clusters. Use the aggregation
framework or map-reduce in sharded environments.
The result set must fit within the maximum BSON document size.
In version 2.2, the returned array can contain at most 20,000 elements;
i.e. at most 20,000 unique groupings. For group by operations that
results in more than 20,000 unique groupings, use
mapReduce
. Previous versions had a limit of 10,000
elements.
Prior to 2.4, the db.collection.group()
method took the
mongod
instance’s JavaScript lock, which blocked all other
JavaScript execution.
mongo
Shell JavaScript Functions/Properties¶
map-reduce operations
, the group
command, and $where
operator expressions cannot access
certain global functions or properties, such as db
, that are
available in the mongo
shell.
The following JavaScript functions and properties are available to
map-reduce operations
, the group
command, and $where
operator expressions:
Available Properties | Available Functions | |
---|---|---|
args MaxKey MinKey |
assert() BinData() DBPointer() DBRef() doassert() emit() gc() HexData() hex_md5() isNumber() isObject() ISODate() isString() |
Map() MD5() NumberInt() NumberLong() ObjectId() print() printjson() printjsononeline() sleep() Timestamp() tojson() tojsononeline() tojsonObject() UUID() version() |
Examples¶
The following examples assume an orders
collection with documents of
the following prototype:
Group by Two Fields¶
The following example groups by the ord_dt
and item.sku
fields those documents that have ord_dt
greater than
01/01/2011
:
The result is an array of documents that contain the group by fields:
The method call is analogous to the SQL statement:
Calculate the Sum¶
The following example groups by the ord_dt
and item.sku
fields, those documents that have ord_dt
greater than
01/01/2011
and calculates the sum of the qty
field for each
grouping:
The result is an array of documents that contain the group by fields and the calculated aggregation field:
The method call is analogous to the SQL statement:
Calculate Sum, Count, and Average¶
The following example groups by the calculated day_of_week
field,
those documents that have ord_dt
greater than 01/01/2011
and
calculates the sum, count, and average of the qty
field for each
grouping:
The result is an array of documents that contain the group by fields and the calculated aggregation field:
See also