group() - Group values JSON command

Groups values for the collection in a query. More command options are available by using the aggregate() command with the $group task.

Syntax diagram

Read syntax diagramSkip visual syntax diagramdb.collection. group(definition ,query,query,limit,query,limit,offset )

Command parameters

definition
This parameter specifies the fields that must be grouped.
query
This parameter specifies a filter for selecting a subset of documents.
limit
This parameter specifies an integer value that indicates the maximum size of the result set.
offset
This parameter finds the document that the query requested after it skips a specified number of rows.

Example

Calculate the average price of books per author:
db.books.group({"_id": {"author": 1}, "sumqty": {"$sum": "$qty"}})
Sample output is as follows:
Row 1:
{
"_id":"Doe",
"sumqty":17481
}
Row 2:
{
"_id":"Smith",
"sumqty":5926
}