$stdDevSamp (Sample StdDev Accumulator)
Last updated: 2026-10-09
$stdDevSamp (Sample StdDev Accumulator)
$stdDevSamp is an accumulator computing the sample standard deviation of a field across a group, dividing by N-1. Use it when data is a sample of a larger population — the common case. Non-numeric values are ignored; the denominator differs from $stdDevPop.
| Category | Accumulators |
|---|---|
| MongoDB Version | 3.2+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $group: { _id: <key>, dev: { $stdDevSamp: "$field" } } }
Returns:Sample standard deviation (non-numeric values ignored)
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $stdDevSamp: sample stddev of scores per class
db.scores.aggregate([
{ $group: { _id: '$class', dev: { $stdDevSamp: '$score' } } }
])
Output:
{ "_id": "A", "dev": 13.02 }
📌 Notes
- Sample stddev divides by N-1
- Null and non-numeric values are ignored
- Needs at least two numeric values per group, else null
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QWhen should I use the sample standard deviation?
AWhen data is a sample of the population, e.g. randomly sampled user behavior.
QCan $stdDevSamp work with two documents?
AAt least two numeric values are required per group; otherwise null.
QDo $stdDevSamp and $stdDevPop agree?
AClose for large datasets, noticeably different for small samples.