$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.

CategoryAccumulators
MongoDB Version3.2+
Example Languagemongosh (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

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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏