View a markdown version of this page

$count (accumulator) - Amazon DocumentDB

$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 $count accumulator 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):

Node.js
const { MongoClient } = require('mongodb'); async function example() { const uri = 'mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'; const client = new MongoClient(uri); try { await client.connect(); const db = client.db('test'); // Accumulator usage: count per group const products = db.collection('products'); await products.insertMany([ { name: "Widget", category: "A" }, { name: "Gadget", category: "A" }, { name: "Doohickey", category: "B" }, { name: "Thingamajig", category: "B" }, { name: "Whatsit", category: "B" } ]); const accumulatorResult = await products.aggregate([ { $group: { _id: "$category", count: { $count: {} } } } ]).toArray(); console.log('Accumulator result:', accumulatorResult); // Window operator usage: running count within each partition const events = db.collection('events'); await 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 } ]); const windowResult = await events.aggregate([ { $setWindowFields: { partitionBy: "$category", sortBy: { day: 1 }, output: { runningCount: { $count: {}, window: { documents: ["unbounded", "current"] } } } } } ]).toArray(); console.log('Window result:', windowResult); } catch (error) { console.error('Error:', error); } finally { await client.close(); } } example();
Python
from pymongo import MongoClient from pprint import pprint def example(): client = None try: client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client['test'] # Accumulator usage: count per group products = db['products'] products.insert_many([ { 'name': 'Widget', 'category': 'A' }, { 'name': 'Gadget', 'category': 'A' }, { 'name': 'Doohickey', 'category': 'B' }, { 'name': 'Thingamajig', 'category': 'B' }, { 'name': 'Whatsit', 'category': 'B' } ]) accumulator_result = list(products.aggregate([ { '$group': { '_id': '$category', 'count': { '$count': {} } } } ])) pprint(accumulator_result) # Window operator usage: running count within each partition events = db['events'] events.insert_many([ { '_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 } ]) window_result = list(events.aggregate([ { '$setWindowFields': { 'partitionBy': '$category', 'sortBy': { 'day': 1 }, 'output': { 'runningCount': { '$count': {}, 'window': { 'documents': ['unbounded', 'current'] } } } } } ])) pprint(window_result) except Exception as e: print(f"An error occurred: {e}") finally: if client: client.close() example()