$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.
| Category | Math |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $sqrt: <numeric-expression> }
⚙️ Parameters
| numeric value | A 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
- Negative input returns null rather than throwing
- Equivalent to $pow: [x, 0.5] but clearer in intent
- Null or missing arguments yield null
🏷️ Related Tags
💡 Related Cases
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.