> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# $count (accumulator operator)

## Definition

Returns the number of documents in a group.

The `$count` accumulator is available in these stages:

- [`$bucket`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/bucket.md#mongodb-pipeline-pipe.-bucket)

- [`$bucketAuto`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/bucketauto.md#mongodb-pipeline-pipe.-bucketAuto)

- [`$group`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/group.md#mongodb-pipeline-pipe.-group)

**Note: Disambiguation**

This page describes the [`$count`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/count-accumulator.md#mongodb-group-grp.-count) aggregation accumulator. You can also use `$count` in these other contexts:

- [`$count (aggregation stage)`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/count.md#mongodb-pipeline-pipe.-count), which returns the number of input documents.

- [`$count (window function)`](https://www.mongodb.com/docs/manual/reference/mql/window-functions/count.md#mongodb-window-window.-count), which is used in the [`$setWindowFields`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/setwindowfields.md#mongodb-pipeline-pipe.-setWindowFields) stage to return the number of documents in a particular window.

## Syntax

```none
{ $count: { } }
```

The `$count` accumulator does not accept any parameters.

**Tip:**

The `$count` accumulator is functionally equivalent to using `{
    $sum: 1 }` within the [`$group`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/group.md#mongodb-pipeline-pipe.-group) stage.

## Examples

The examples on this page use data from the [sample\_mflix dataset](https://www.mongodb.com/docs/manual/sample-data/sample-mflix.md#std-label-sample-mflix). For details on how to load this dataset into your self-managed MongoDB deployment, see [Load the Sample Dataset](https://www.mongodb.com/docs/manual/sample-data/load-sample-data-local.md#std-label-sample-dataset-local). If you made any modifications to the sample databases, you may need to drop and recreate the databases to run the examples on this page.

This example uses [`$count`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/count-accumulator.md#mongodb-group-grp.-count) in the [`$group`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/group.md#mongodb-pipeline-pipe.-group) stage to count the number of movies in the `sample_mflix.movies` collection for each selected `genre`:

```shell
db.movies.aggregate( [
   { $unwind: "$genres" },
   { $match: { genres: { $in: [ "Drama", "Short" ] } } },
   {
      $group: {
         _id: "$genres",
         countNumberOfMoviesForGenre: {
            $count: {}
         }
      }
   },
   { $sort: { _id: 1 } }
] )

```

**Output:**

```shell
[
   { _id: 'Drama', countNumberOfMoviesForGenre: 12385 },
   { _id: 'Short', countNumberOfMoviesForGenre: 442 }
]

```

In the example:

- `$unwind: "$genres"` creates a document for each value in the `genres` array.

- `$match` limits the documents to the `Drama` and `Short` genres.

- `_id: "$genres"` groups the documents by genre.

- `$count: {}` sets the `countNumberOfMoviesForGenre` field to the number of movies that share the same genre.

The output shows the number of movies for `Drama` and `Short` in the `countNumberOfMoviesForGenre` field.
