$count (accumulator)
New from version 8.0.1.
Use the $count accumulator within a $group stage to return the number of documents in each group. It accepts an empty object {} as its argument.
This is different from the $count pipeline stage, which is a standalone stage that counts all documents passing through the pipeline. The $count accumulator instead counts documents within each group produced by $group.
Syntax
{ $count: {} }
Parameters
-
The
$countaccumulator takes no arguments. It accepts an empty object{}and returns the count of documents in each group.
Example (MongoDB Shell)
The following example demonstrates how to use the $count accumulator to count the number of products in each category.
Create sample documents
db.products.insertMany([ { name: "Widget", category: "A" }, { name: "Gadget", category: "A" }, { name: "Doohickey", category: "B" }, { name: "Thingamajig", category: "B" }, { name: "Whatsit", category: "B" } ])
Query example
db.products.aggregate([ { $group: { _id: "$category", count: { $count: {} } } } ])
Output
[
{ "_id": "A", "count": 2 },
{ "_id": "B", "count": 3 }
]
Code examples
To view a code example for using the $count accumulator, choose the tab for the language that you want to use: