- Reference >
- JavaScript Methods >
- db.collection.group()
db.collection.group()¶
-
db.collection.
group
({ key, reduce, initial, [keyf,] [cond,] finalize })¶ The
db.collection.group()
method 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 aSELECT .. GROUP BY
statement in SQL. Thegroup()
method returns an array.The
db.collection.group()
accepts a single document that contains the following:Fields: - key – Specifies one or more document fields to group by.
- reduce – Specifies a function for the group operation perform on the documents during the grouping operation, such as compute a sum or a count. The aggregation function takes two arguments: the current document and the aggregate result for the previous documents in the.
- initial – Initializes the aggregation result document.
- keyf – Optional. Alternative to the
key
field. Specifies a function that creates a “key object” for use as the grouping key. Use thekeyf
instead ofkey
to group by calculated fields rather than existing document fields. - cond – Optional. Specifies 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 – Optional. Specifies 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.Warning
- The
db.collection.group()
method does not work with sharded clusters. Use the aggregation framework or map-reduce in sharded environments. - The
group
command takes a read lock and does not allow any other threads to execute JavaScript while it is running.
Note
- 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.
Consider the following examples of the db.collection.group()
method:
The examples assume an orders
collection with documents of the
following prototype:
The following example groups by the
ord_dt
anditem.sku
fields those documents that haveord_dt
greater than01/01/2011
:The result is an array of documents that contain the group by fields:
The method call is analogous to the SQL statement:
The following example groups by the
ord_dt
anditem.sku
fields, those documents that haveord_dt
greater than01/01/2011
and calculates the sum of theqty
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:
The following example groups by the calculated
day_of_week
field, those documents that haveord_dt
greater than01/01/2011
and calculates the sum, count, and average of theqty
field for each grouping:The result is an array of documents that contain the group by fields and the calculated aggregation field:
See also