$sqrt (aggregation)
On this page
Definition
-
$sqrt
-
New in version 3.2.
Calculates the square root of a positive number and returns the result as a double.
$sqrt
has the following syntax:The argument can be any valid expression as long as it resolves to a non-negative number. For more information on expressions, see Expressions.
Behavior
If the argument resolves to a value of null
or refers to a field that is missing, $sqrt
returns null
. If the argument resolves to NaN
, $sqrt
returns NaN
.
$sqrt
errors on negative numbers.
Example | Results |
---|---|
{ $sqrt: 25 } |
5 |
{ $sqrt: 30 } |
5.477225575051661 |
{ $sqrt: null } |
null |
Example
A collection points
contains the following documents:
The following example uses $sqrt
to calculate the distance between p1
and p2
:
The operation returns the following results: