Definition
Returns the sample standard deviation of values in a window.
Note
Disambiguation
This page describes $stdDevSamp when used as a window
function.
You can also use $stdDevSamp in these other contexts:
$stdDevSamp (accumulator), which calculates the sample standard deviation of values across a group of documents.$stdDevSamp (expression), which calculates the sample standard deviation of numeric values in an array.
Syntax
{ $setWindowFields: { ... output: { <field>: { $stdDevSamp: <expression> } } } }
Behavior
Result Type
$stdDevSamp returns the sample standard deviation of the input values as a double.
Non-Numeric and Missing Values
The $stdDevSamp window function ignores non-numeric values, including missing values. If the window contains no numeric values, $stdDevSamp returns null.
Single Value
If the window contains only one document, $stdDevSamp returns null.
Example
The examples on this page use data from the sample_mflix dataset. For details on how to load this dataset into your self-managed MongoDB deployment, see Load the Sample Dataset. 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 $setWindowFields to calculate a running sample standard deviation field for each Musical movie from 2009 and 2010. Each document receives the $stdDevSamp value computed from the beginning of its yearly partition through the current document.
db.movies.aggregate( [ { $match: { genres: "Musical", year: { $in: [ 2009, 2010 ] }, "imdb.rating": { $type: "double" } } }, { $setWindowFields: { partitionBy: "$year", sortBy: { title: 1 }, output: { stdDevSampRatingInYear: { $stdDevSamp: "$imdb.rating", window: { documents: [ "unbounded", "current" ] } }, rankInYear: { $documentNumber: {} } } } }, { $project: { _id: 0, title: 1, year: 1, "imdb.rating": 1, stdDevSampRatingInYear: 1, rankInYear: 1 } }, { $sort: { year: 1, title: 1 } } ] )
The preceding pipeline:
Uses
$matchto filter for Musical movies from 2009 and 2010 with a numeric IMDb rating.Uses
$setWindowFieldsto partition movies byyearand sort them bytitle. The stage uses$stdDevSampwith a documents window of[ "unbounded", "current" ]to create the following fields for each document:stdDevSampRatingInYear: the running sample standard deviation ofimdb.rating.rankInYear: a per-partition row number assigned by$documentNumber.
The first document in each partition has only one value in its window, so
stdDevSampRatingInYearisnull. Each subsequent document adds one more value to the window.Uses
$projectto includetitle,year,imdb,stdDevSampRatingInYear, andrankInYearin the output.Uses
$sortto sort results by year and title.