$sqrt (Square Root)

Last updated: 2026-10-09

$sqrt (Square Root)

$sqrt returns the square root of a single numeric expression. It is used in geometry, standard-deviation-style math, and normalization, such as taking the square root of a sum of squares when computing Euclidean distance. A null or missing argument yields null, and negative input also returns null since negative numbers have no real square root. $sqrt is equivalent to $pow: [x, 0.5] but reads more clearly. It supports integers, doubles, and Decimals.

CategoryMath
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

{ $sqrt: <numeric-expression> }

⚙️ Parameters

numeric valueA numeric value or an expression resolving to one; must be non-negative.

Returns:The square root of the input; null for negative, null, or missing input

▶ Example

Run the commands below in your local mongosh (requires a running mongod):

// $sqrt: distance from two legs of a right triangle
db.points.aggregate([
  { $project: {
      distance: { $sqrt: { $add: [{ $pow: ['$x', 2] }, { $pow: ['$y', 2] }] } }
  } }
])

Output:

{ '_id': 1, 'distance': 5 }
{ '_id': 2, 'distance': 10 }

📌 Notes

More hands-on cases coming soon.

📚 Related Tutorials

❓ FAQ

QWhat does $sqrt return for negative input?
AIt returns null; negative numbers have no real square root.
QHow do $sqrt and $pow relate?
A$sqrt(x) equals $pow: [x, 0.5].
QDoes $sqrt support Decimal?
AYes, a Decimal input returns a Decimal result.
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%

🙏 帮我们做得更好

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

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