In MongoDB, the $acos
aggregation pipeline operator returns the arccosine (inverse cosine) of a value, measured in radians.
$acos
accepts any valid expression that resolves to a number between -1
and 1
.
The $acos
operator was introduced in MongoDB 4.2.
Example
Suppose we have a collection called test
with the following document:
{ "_id" : 1, "data" : 0.5 }
We can use the $acos
operator to return the arccosine of the data
field:
db.test.aggregate(
[
{ $project: {
_id: 0,
arccosine: { $acos: "$data" }
}
}
]
)
Result:
{ "arccosine" : 1.0471975511965976 }
By default, the $acos
operator returns values as a double
, but it can also return values as a 128-bit decimal as long as the expression resolves to a 128-bit decimal value.
Pi
Providing -1 resolves to the mathematical constant pi (π).
Suppose we add the following document to our collection, which contains a data value of -1
.
{ "_id" : 2, "data" : -1 }
Now let’s run the $acos
operator against that field:
db.test.aggregate(
[
{ $match: { _id: 2 } },
{ $project: {
_id: 0,
arccosine: { $acos: "$data" }
}
}
]
)
Result:
{ "arccosine" : 3.141592653589793 }
This result is π to 15 decimal places.
Convert to Degrees
As mentioned, $acos
returns its result in radians. You can use the $radiansToDegrees
operator if you want the result in degrees.
Example:
db.test.aggregate(
[
{ $match: { _id: 2 } },
{ $project: {
_id: 0,
radians: { $acos: "$data" },
degrees: { $radiansToDegrees: { $acos: "$data" } }
}
}
]
)
Result:
{ "radians" : 3.141592653589793, "degrees" : 180 }
In this example, the first field presents the result in radians, and the second field presents it in degrees.
Null Values
Null values return null
when using the $acos
operator.
Suppose we add the following document to our collection:
{ "_id" : 3, "data" : null }
Let’s run the the $acos
operator against that document:
db.test.aggregate(
[
{ $match: { _id: 3 } },
{ $project: {
_id: 0,
arccosine: { $acos: "$data" }
}
}
]
)
Result:
{ "arccosine" : null }
We can see that the result is null
.
NaN Values
If the argument resolves to NaN
, $acos
returns NaN
.
Example:
db.test.aggregate(
[
{ $match: { _id: 3 } },
{ $project: {
_id: 0,
arccosine: { $acos: 1 * "oops!" }
}
}
]
)
Result:
{ "arccosine" : NaN }
In this case I tried to multiple a number by a string, which resulted in NaN
being returned.
Infinity
If the argument resolves to Infinity
or -Infinity
, the $acos
operator returns an error.
Suppose we add the following document to our collection:
{ "_id" : 4, "data" : Infinity }
Let’s run $acos
against the data field:
db.test.aggregate(
[
{ $match: { _id: 4 } },
{ $project: {
_id: 0,
arccosine: { $acos: "$data" }
}
}
]
)
Result:
uncaught exception: Error: command failed: { "ok" : 0, "errmsg" : "cannot apply $acos to inf, value must in [-1,1]", "code" : 50989, "codeName" : "Location50989" } : aggregate failed : _getErrorWithCode@src/mongo/shell/utils.js:25:13 doassert@src/mongo/shell/assert.js:18:14 _assertCommandWorked@src/mongo/shell/assert.js:618:17 assert.commandWorked@src/mongo/shell/assert.js:708:16 DB.prototype._runAggregate@src/mongo/shell/db.js:266:5 DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1046:12 @(shell):1:1
That’s the error I get in the mongo
shell when running that code.
Non-Existent Fields
If the $acos
operator is applied against a field that doesn’t exist, null
is returned.
Example:
db.test.aggregate(
[
{ $match: { _id: 4 } },
{ $project: {
_id: 0,
arccosine: { $acos: "$oops" }
}
}
]
)
Result:
{ "arccosine" : null }
Out of Range Values
Providing an out of range value to $acos
will result in an error.
Suppose we add the following document to the collection:
{ "_id" : 5, "data" : 2 }
Now let’s run $acos
against the data
field:
db.test.aggregate(
[
{ $match: { _id: 5 } },
{ $project: {
_id: 0,
radians: { $acos: "$data" }
}
}
]
)
Result:
uncaught exception: Error: command failed: { "ok" : 0, "errmsg" : "cannot apply $acos to 2, value must in [-1,1]", "code" : 50989, "codeName" : "Location50989" } : aggregate failed : _getErrorWithCode@src/mongo/shell/utils.js:25:13 doassert@src/mongo/shell/assert.js:18:14 _assertCommandWorked@src/mongo/shell/assert.js:618:17 assert.commandWorked@src/mongo/shell/assert.js:708:16 DB.prototype._runAggregate@src/mongo/shell/db.js:266:5 DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1046:12 @(shell):1:1
As the error message alludes to, the value must be between -1
and 1
.