The $stdDevSamp accumulator measures sample standard deviation in each $group bucket—how far numeric values spread when your data is a sample meant to represent a larger population.
01
Sample spread
N−1 divisor formula.
02
Simple syntax
{ $stdDevSamp: "$field" }
03
vs $stdDevPop
Sample vs population.
04
+ $avg
Mean and spread together.
05
One value → null
Needs at least two points.
06
With $sample
Random subset analysis.
Fundamentals
Definition and Usage
$stdDevSamp is a MongoDB accumulator used in the $group stage. For each group, it evaluates a numeric expression on every document and returns the sample standard deviation—spread calculated with Bessel’s correction (divide variance by N−1).
💡
Beginner tip
Ask: “Is this group all the data I care about, or just a slice of something bigger?” All the data → $stdDevPop. A slice meant to represent more → $stdDevSamp.
Use $stdDevSamp after $sample, for survey subsets, or when each group holds partial readings you want to extrapolate. When the group is the complete dataset, use $stdDevPop instead.
In production, pair $sample before $group to analyze a random subset of a large collection—then $stdDevSamp estimates population spread from that sample.
Example 5 — Single value returns null; round multi-value results
$stdDevSamp estimates spread when your grouped data is a sample, not the full population. Use it with $sample, survey subsets, or any partial dataset you want to generalize from.
When the group contains every value you care about, use $stdDevPop instead. Next: $sum for totaling numeric values per group.
Use $stdDevSamp when data is a sample of a larger population
Pair with $sample for random-subset statistical analysis
Report $sum: 1 (count) alongside std dev for context
Compare $stdDevPop and $stdDevSamp side by side when teaching or validating
Round with $round for dashboard-friendly output
❌ Don’t
Use $stdDevSamp on complete census data—use $stdDevPop
Expect a result from groups with only one numeric value—it returns null
Confuse sample (N−1) and population (N) formulas
Compare std dev across groups with very different counts without showing N
Pass array fields directly in $group—they are ignored as non-numeric
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $stdDevSamp
Use these points when estimating spread from sample data in $group.
5
Core concepts
📊01
Sample σ
N−1 divisor.
Basics
📝02
Simple syntax
{ $stdDevSamp: "$field" }
Syntax
📈03
vs $stdDevPop
Sample vs population.
Compare
🎯04
≥ 2 values
One → null.
Edge case
💲05
+ $sample
Random subsets.
Pattern
❓ Frequently Asked Questions
$stdDevSamp calculates the sample standard deviation of numeric values in each $group bucket. It estimates how spread out values are when your grouped data is a sample meant to represent a larger population.
Inside $group: { spread: { $stdDevSamp: "$score" } }. It also works in $project, $addFields, and window stages—but this tutorial focuses on the $group accumulator.
$stdDevSamp divides variance by N−1 (sample formula). $stdDevPop divides by N (population formula). Use $stdDevSamp when inferring about a bigger population from a subset.
Non-numeric values—including null, missing fields, strings, and objects—are ignored. If no numeric values exist in a group, $stdDevSamp returns null.
$stdDevSamp returns null. Sample standard deviation requires at least two data points (divisor N−1 would be zero with one value).
Choose $stdDevSamp when your group is a random subset (e.g. after $sample) or partial data you want to generalize from. Choose $stdDevPop when the group is the complete population you care about.
Did you know?
For quiz 1 scores 85, 90, and 71, $stdDevPop returns ~8.04 while $stdDevSamp returns ~9.85—the sample formula is slightly higher because dividing by N−1 instead of N compensates for using a subset to estimate a larger population. See the official $stdDevSamp docs.