MongoDB $acos Operator

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 4 Examples
Trigonometry

What You’ll Learn

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.

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.

📝 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.51.047 rad (valid)
$acos: 10 rad (valid edge)
$acos: 1.5NaN (out of range!)

⚡ Quick Reference

QuestionAnswer
Operator typeAggregation expression operator (trigonometry)
Syntax{ $acos: <expression> }
Input range-1 to 1 (inclusive)
Output unitRadians (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

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:

mongosh
[
  { "_id": ObjectId("609c26812e9274a86871bc6a"), "cosine": 0.5 },
  { "_id": ObjectId("609c26812e9274a86871bc6b"), "cosine": 0.866 },
  { "_id": ObjectId("609c26812e9274a86871bc6c"), "cosine": 0.707 }
]

Example 1 — Basic $acos on a Field

Compute the angle in radians for each cosine value:

mongosh
db.angles.aggregate([
  {
    $project: {
      angleRadians: { $acos: "$cosine" }
    }
  }
])

How It Works

  • Document 1: cosine is 0.5acos(0.5)1.047 rad (60°).
  • Document 2: cosine is 0.866acos(0.866)0.524 rad (30°).
  • Document 3: cosine is 0.707acos(0.707)0.785 rad (45°).

📈 Practical Patterns

Convert radians to degrees and handle common trigonometric edge cases in pipelines.

Example 2 — Convert Radians to Degrees

MongoDB returns radians by default. Multiply by 180 / π to get degrees:

mongosh
db.angles.aggregate([
  {
    $project: {
      cosine: 1,
      angleRadians: { $acos: "$cosine" },
      angleDegrees: {
        $multiply: [
          { $acos: "$cosine" },
          { $divide: [ 180, 3.141592653589793 ] }
        ]
      }
    }
  }
])

How It Works

The formula is degrees = radians × (180 / π). Use $multiply and $divide to apply it inside the pipeline.

Example 3 — Angle from a Normalized Vector

When you have x/y components of a unit vector, compute the angle from the x-axis:

mongosh
db.vectors.aggregate([
  {
    $project: {
      label: 1,
      x: 1,
      y: 1,
      angleFromXAxis: { $acos: "$x" }
    }
  }
])

How It Works

For a unit vector, the x-component equals cos(θ). Applying $acos on $x recovers the angle from the positive x-axis.

Example 4 — $acos with Literal Values

Common boundary values every beginner should know:

mongosh
db.samples.aggregate([
  {
    $project: {
      acosOne:  { $acos: 1 },    // 0 rad (0°)
      acosZero: { $acos: 0 },    // π/2 rad (90°)
      acosNeg:  { $acos: -1 }    // π rad (180°)
    }
  }
])

How It Works

$acos(1) = 0, $acos(0) = π/2, and $acos(-1) = π. These edge cases are useful for validating pipeline logic.

🚀 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.

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.

💡 Best Practices

✅ Do

  • Validate that input values are between -1 and 1
  • Remember output is in radians, not degrees
  • Use $round when displaying angle results
  • Pair with $cos to verify round-trip calculations
  • Handle null inputs with $ifNull when needed

❌ Don’t

  • Pass values outside the -1 to 1 range
  • Assume output is in degrees
  • Use $acos as a query filter outside expressions
  • Forget that out-of-range input returns NaN
  • Mix up $acos (inverse cosine) with $cos (cosine)

Key Takeaways

Knowledge Unlocked

Five things to remember about $acos

Use these points when working with trigonometric data in MongoDB.

5
Core concepts
📝 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.

Continue the Math Operator Series

Move on to $acosh or review $abs for absolute value calculations.

Next: $acosh →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful