$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 shows 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 }
]
Window operator usage example (MongoDB Shell)
New from version 8.0.2.
The $count accumulator can also be used as a window operator in the $setWindowFields stage. In this context, it returns the number of documents in each window. You specify the operator under the output field, and optionally define the window boundaries with a window document.
Create sample documents
db.events.insertMany([ { _id: 1, category: "A", day: 1 }, { _id: 2, category: "A", day: 2 }, { _id: 3, category: "A", day: 3 }, { _id: 4, category: "B", day: 1 }, { _id: 5, category: "B", day: 2 } ])
Query example
The following example partitions the documents by category, sorts each partition by day, and returns a running count of documents from the start of the partition through the current document.
db.events.aggregate([ { $setWindowFields: { partitionBy: "$category", sortBy: { day: 1 }, output: { runningCount: { $count: {}, window: { documents: ["unbounded", "current"] } } } } } ])
Output
[
{ "_id": 1, "category": "A", "day": 1, "runningCount": 1 },
{ "_id": 2, "category": "A", "day": 2, "runningCount": 2 },
{ "_id": 3, "category": "A", "day": 3, "runningCount": 3 },
{ "_id": 4, "category": "B", "day": 1, "runningCount": 1 },
{ "_id": 5, "category": "B", "day": 2, "runningCount": 2 }
]
Each document is augmented with runningCount, the number of documents within its partition up to and including the current document.
Code examples
To view a code example for using the $count accumulator, choose the tab for the language that you want to use. The following examples show both accumulator usage (in $group) and window operator usage (in $setWindowFields):