Sample Input Documents
Suppose you have a numbers collection with a value field:
[
{ "_id": 1, "value": 9 },
{ "_id": 2, "value": 16 },
{ "_id": 3, "value": 25 },
{ "_id": 4, "value": 2 }
] 
The $sqrt operator calculates the square root of a number in MongoDB aggregation pipelines. Use it for geometric calculations, data normalization, statistical analysis, and anywhere you need √x inside a query.
√x from any field.
One numeric expression.
Add computed fields.
Distance and area.
Stats, normalization.
Clearer than 0.5.
In MongoDB’s aggregation framework, the $sqrt operator computes the square root of a numeric expression. For example, { $sqrt: 9 } returns 3, and { $sqrt: 25 } returns 5.
This is useful when you need mathematical operations inside a pipeline — such as computing distances from coordinate differences, deriving side lengths from areas, or transforming skewed numeric data.
Think of $sqrt as MongoDB’s version of JavaScript’s Math.sqrt(). It works on non-negative numbers; negative inputs return null.
The $sqrt operator takes one numeric expression:
{ $sqrt: <expression> } { $sqrt: 9 }
// Result: 3
{ $sqrt: 2 }
// Result: 1.4142135623730951
{ $sqrt: "$value" }
// Square root of the value field $sqrt — returns the square root of the expression that follows.<expression> — can be a field path, literal number, or nested numeric expression.$project, $addFields, or $set.null (no real square root).null input — returns null.0.{ $sqrt: "$x" }{ $pow: [ "$x", 0.5 ] }$sqrt for readability.| Question | Answer |
|---|---|
| Operator type | Aggregation expression operator (math) |
| Syntax | { $sqrt: <expression> } |
| Input | Non-negative number or numeric expression |
| Output | Square root (number) or null |
| Negative input | null |
| Common stages | $project, $addFields, $set |
{
$sqrt: 16
}Returns 4
{
$sqrt: "$value"
}Field square root
{
$sqrt: -4
}Returns null
$sqrt: { $add: [...] }Expression input
Compute square roots on document fields, calculate geometric distances, and compare with $pow.
Start with a numbers collection and compute square roots with $project.
Suppose you have a numbers collection with a value field:
[
{ "_id": 1, "value": 9 },
{ "_id": 2, "value": 16 },
{ "_id": 3, "value": 25 },
{ "_id": 4, "value": 2 }
] Use $project to add a sqrtValue field:
db.numbers.aggregate([
{
$project: {
value: 1,
sqrtValue: { $sqrt: "$value" }
}
}
]) value is 9, so sqrtValue is 3.value is 16, so sqrtValue is 4.value is 25, so sqrtValue is 5.value is 2, so sqrtValue is approximately 1.414.Apply $sqrt to real-world distance and normalization problems.
Calculate distance from coordinate differences using $sqrt with $add and $pow:
db.points.aggregate([
{
$project: {
name: 1,
distance: {
$sqrt: {
$add: [
{ $pow: [ { $subtract: [ "$x2", "$x1" ] }, 2 ] },
{ $pow: [ { $subtract: [ "$y2", "$y1" ] }, 2 ] }
]
}
}
}
}
])
// (x1,y1)=(0,0), (x2,y2)=(3,4) → distance = 5 Square the differences in x and y, add them, then take the square root. This is the classic √(dx² + dy²) formula for distance between two points.
If you store the area of a square, derive the side length:
db.plots.aggregate([
{
$project: {
plotId: 1,
areaSqM: 1,
sideLength: { $sqrt: "$areaSqM" }
}
}
])
// areaSqM: 144 → sideLength: 12
// areaSqM: 81 → sideLength: 9 For a square, side = √area. This pattern works whenever a stored value is the square of the quantity you need.
Square roots can reduce skew in variance-based calculations. A simplified normalization step:
db.metrics.aggregate([
{
$project: {
sensor: 1,
variance: 1,
stdDevApprox: { $sqrt: "$variance" }
}
}
]) Standard deviation is the square root of variance. If variance is already stored, $sqrt gives you the spread measure directly.
Both approaches compute the same square root; $sqrt reads more clearly:
db.numbers.aggregate([
{
$project: {
value: 1,
viaSqrt: { $sqrt: "$value" },
viaPow: { $pow: [ "$value", 0.5 ] }
}
}
]) Raising to the power of 0.5 is mathematically equivalent to a square root. Prefer $sqrt when that is all you need.
$sqrt WorksThe pipeline evaluates the input — a field like "$value" or a numeric expression.
Non-negative numbers proceed. Negative numbers return null; null input stays null.
MongoDB returns the principal (non-negative) square root as a floating-point number when needed.
The value is stored in the field you define in $project or $addFields.
The $sqrt operator is a straightforward math tool in MongoDB aggregation pipelines. It computes square roots for geometric calculations, statistical transforms, and data normalization.
For beginners, remember: wrap any non-negative numeric expression in { $sqrt: ... } inside a stage like $project. Next in the series: $strcasecmp.
$sqrt for readability when you only need a square root$pow and $add for distance formulas$sqrt$ifNull when fields may be missing$round when displaying to usersnull$sqrt with $split (string vs math operator)$sqrt as a query filter outside expression stages$sqrtUse these points when writing your next aggregation pipeline.
√x in pipelines.
Purpose{ $sqrt: expr }
No real root.
Edge caseDistance & area.
Use casePrefer $sqrt.
CompareMove on to $strcasecmp for case-insensitive string comparison, or review $pow for exponent math.
5 people found this page helpful