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

# $stdDevPop (accumulator operator)

## Definition

Calculates the population standard deviation of values across a group of documents. Use `$stdDevPop` if the values encompass the entire population of data you want to represent and you do not want to generalize.

If the values represent only a sample from the population, use [`$stdDevSamp`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/stddevsamp.md#mongodb-group-grp.-stdDevSamp) instead.

The `$stdDevPop` 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 `$stdDevPop` when used as an **accumulator**. Accumulators return an aggregated value across a group of input documents.

You can also use `$stdDevPop` in these other contexts:

- [`$stdDevPop (expression)`](https://www.mongodb.com/docs/manual/reference/expression/stddevpop.md#mongodb-expression-exp.-stdDevPop), which returns the population standard deviation of numeric values in an array.

- [`$stdDevPop (window function)`](https://www.mongodb.com/docs/manual/reference/mql/window-functions/stddevpop.md#mongodb-window-window.-stdDevPop), which is used in the [`$setWindowFields`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/setwindowfields.md#mongodb-pipeline-pipe.-setWindowFields) stage to calculate the population standard deviation of values from documents in a particular window.

## Syntax

```javascript
{ $stdDevPop: <expression> }
```

For more information on expressions, see [Expressions.](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions)

## Behavior

### Result Type

`$stdDevPop` returns the population standard deviation of the input values as a [`double`.](https://www.mongodb.com/docs/manual/reference/mongodb-extended-json.md#mongodb-bsontype-Double)

### Non-numeric or Missing Values

`$stdDevPop` ignores non-numeric values, including missing values. If all input values are non-numeric, `$stdDevPop` returns `null`.

### Single Value

If the input consists of a single numeric value, `$stdDevPop` returns `0`.

## Example

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.

The following example groups movies by director and returns the five directors with the most varied IMDb ratings, among directors with at least 10 movies.

```javascript
db.movies.aggregate( [
   {
      $match: {
         directors: { $exists: true },
         "imdb.rating": { $type: "double" }
      }
   },
   {
      $unwind: "$directors"
   },
   {
      $group: {
         _id: "$directors",
         movieCount: { $sum: 1 },
         stdDevRating: { $stdDevPop: "$imdb.rating" }
      }
   },
   {
      $match: { movieCount: { $gte: 10 } }
   },
   {
      $sort: { stdDevRating: -1 }
   },
   {
      $limit: 5
   }
] )

```

**Output:**

```javascript
[
  { _id: 'Bob Clark', movieCount: 12, stdDevRating: 1.6436747447918845 },
  { _id: 'John McTiernan', movieCount: 10, stdDevRating: 1.439305388025766 },
  { _id: 'James Cameron', movieCount: 10, stdDevRating: 1.3683566786477859 },
  { _id: 'Sean McNamara', movieCount: 13, stdDevRating: 1.3499945211372495 },
  {
    _id: 'Robert Rodriguez',
    movieCount: 20,
    stdDevRating: 1.2701476292148088
  }
]

```

The preceding pipeline:

- Uses [`$match`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/match.md#mongodb-pipeline-pipe.-match) to filter for movies that have a `directors` field and a numeric IMDb rating.

- Uses [`$unwind`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/unwind.md#mongodb-pipeline-pipe.-unwind) to create one document per director per movie, so each director-movie pair is grouped separately.

- Uses [`$group`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/group.md#mongodb-pipeline-pipe.-group) to group by director, count movies, and compute `stdDevRating` using `$stdDevPop` on `imdb.rating`.

- Uses [`$match`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/match.md#mongodb-pipeline-pipe.-match) to keep only directors with at least 10 movies.

- Uses [`$sort`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/sort.md#mongodb-pipeline-pipe.-sort) to return results in descending order by `stdDevRating`.

- Uses [`$limit`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/limit.md#mongodb-pipeline-pipe.-limit) to return the top 5 results.

## Learn More

- [Accumulators](https://www.mongodb.com/docs/manual/reference/mql/accumulators.md#std-label-accumulators-ref)

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

- [Aggregation Pipeline Limits](https://www.mongodb.com/docs/manual/core/aggregation-pipeline-limits.md#std-label-agg-pipeline-limits)
