The $acos operator returns the arc cosine (inverse cosine) of a number in MongoDB aggregation pipelines. If you know a cosine value, $acos tells you the angle in radians.
01
Inverse Cosine
Cosine value → angle.
02
Syntax
One expression inside $acos.
03
Radians
Output is always in radians.
04
Input Range
Values must be -1 to 1.
05
Use Cases
Geometry, robotics, signals.
06
Degrees
Convert with $multiply/$divide.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $acos operator computes the arccosine of a numeric expression. For example, if cos(60°) = 0.5, then $acos(0.5) returns approximately 1.047 radians (which is 60°). This is useful when your data stores cosine values and you need to recover the original angle.
💡
Beginner Tip
Think of $acos as the reverse of $cos. Use it inside aggregation expression stages like $project or $addFields, not as a standalone query filter.
Foundation
📝 Syntax
The $acos operator takes one numeric expression:
mongosh
{ $acos: <expression> }
Syntax Rules
$acos — returns the arc cosine of the expression in radians.
<expression> — must evaluate to a number between -1 and 1 (inclusive).
Use it inside stages like $project, $addFields, or $set.
If the input is null, the result is null.
Values outside [-1, 1] return NaN.
⚠️ Valid Input Range: -1 to 1
$acos only accepts cosine values in the range -1 to 1. Anything outside that range returns NaN.
$acos: 0.5 → 1.047 rad (valid)
$acos: 1 → 0 rad (valid edge)
$acos: 1.5 → NaN(out of range!)
Cheat Sheet
⚡ Quick Reference
Question
Answer
Operator type
Aggregation expression operator (trigonometry)
Syntax
{ $acos: <expression> }
Input range
-1 to 1 (inclusive)
Output unit
Radians (range 0 to π)
Common stages
$project, $addFields, $set
Example
{
$acos: 0.5
}
≈ 1.047 rad (60°)
Field
{
$acos: "$cosine"
}
Angle from cosine field
Edge case
{
$acos: 1
}
Returns 0 radians
Null input
{
$acos: null
}
Returns null
Hands-On
Examples Gallery
Start with cosine values, run an aggregation pipeline, and see the resulting angles in radians.
📚 Cosine to Angle
Use an angles collection with stored cosine values and recover the angle in radians with $project.
Sample Input Documents
Suppose you have an angles collection where each document stores a cosine value:
$acos(1) = 0, $acos(0) = π/2, and $acos(-1) = π. These edge cases are useful for validating pipeline logic.
Applications
🚀 Use Cases
Geometric computations — recover angles from cosine values in triangle or vector problems.
Robotics and engineering — calculate joint angles, orientations, and motion paths from sensor data.
Trigonometric analysis — transform stored trig values for signal processing or visualization pipelines.
Navigation and mapping — derive heading angles when direction is stored as a cosine component.
🧠 How $acos Works
1
MongoDB reads the cosine value
The pipeline evaluates the input — a field like "$cosine" or a numeric expression between -1 and 1.
Input
2
$acos computes the inverse
MongoDB applies the arccosine function and returns the angle in radians (0 to π).
Transform
3
The result is stored in the pipeline
The angle is written to the field you define in $project or $addFields.
Output
=
📐
Angle recovered from data
You can use the radians directly or convert to degrees for reports and dashboards.
Wrap Up
Conclusion
The $acos operator is a practical trigonometry tool in MongoDB aggregation pipelines. It converts cosine values back into angles, which is essential for geometry, engineering, robotics, and any dataset that stores trig components instead of raw angles.
For beginners, remember three things: input must be between -1 and 1, output is in radians, and you can convert to degrees with a simple multiply/divide step.
Use these points when working with trigonometric data in MongoDB.
5
Core concepts
📐01
Inverse Cosine
Cosine → angle in radians.
Purpose
📝02
Simple Syntax
{ $acos: expr }
Syntax
📏03
Input Range
-1 to 1 only.
Domain
🛠04
Radians Output
Convert to degrees if needed.
Units
⚠05
Out of Range
Values > 1 return NaN.
Edge case
❓ Frequently Asked Questions
$acos returns the arc cosine (inverse cosine) of a number. It answers the question: which angle has this cosine value? The result is always in radians.
The syntax is { $acos: <expression> }. The expression can be a field reference like "$cosine", a literal number, or another numeric expression.
$acos only works for values between -1 and 1 inclusive. Values outside that range return NaN.
$acos returns radians. To convert to degrees, multiply the result by 180 and divide by Math.PI using $multiply and $divide.
$acos returns null when the input is null. It does not throw an error.