MongoDB $atan Operator

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

What You’ll Learn

The $atan operator returns the arc tangent (inverse tangent) of a number in MongoDB aggregation pipelines. If you know a slope or tangent ratio, $atan tells you the angle in radians.

01

Inverse Tangent

Slope → angle.

02

Syntax

One expression inside $atan.

03

Radians

Output is always in radians.

04

Any Real Input

No -1 to 1 limit.

05

Use Cases

Geometry, vectors, engineering.

06

$atan2

Full quadrant angles.

Definition and Usage

In MongoDB’s aggregation framework, the $atan operator computes the arc tangent of a numeric expression. For example, if tan(45°) = 1, then $atan(1) returns approximately 0.785 radians (which is 45°). The input is a tangent ratio (slope), not an angle — and the output is the angle in radians.

💡
Beginner Tip

Think of $atan as the reverse of $tan. A common pattern is { $atan: { $divide: ["$y", "$x"] } } to get an angle from vector components — but for full quadrant accuracy, use $atan2 instead.

📝 Syntax

The $atan operator takes one numeric expression:

mongosh
{ $atan: <expression> }

Syntax Rules

  • $atan — returns the arc tangent of the expression in radians.
  • <expression> — can be any real number (tangent ratio or slope).
  • Use it inside stages like $project, $addFields, or $set.
  • If the input is null, the result is null.
  • Output range is approximately -π/2 to π/2 radians.

⚠️ $atan vs $atan2

$atan(y/x) works for simple slopes but has two limitations:

x = 0 — division fails or returns null
Quadrants$atan cannot distinguish all four quadrants
Example: atan(2/-2) and atan(-1/1) both ≈ -0.79 rad
Use $atan2 when you need the correct angle from separate x and y values

⚡ Quick Reference

QuestionAnswer
Operator typeAggregation expression operator (trigonometry)
Syntax{ $atan: <expression> }
Input rangeAny real number
Output unitRadians (range -π/2 to π/2)
Common stages$project, $addFields, $set
Example
{
  $atan: 1
}

≈ 0.785 rad (45°)

Slope
{
  $atan: "$slope"
}

Angle from slope field

Edge case
{
  $atan: 0
}

Returns 0 radians

Null input
{
  $atan: null
}

Returns null

Examples Gallery

Compute angles from slopes and vector components using $atan in aggregation pipelines.

📚 Slope to Angle

Use a vectors collection with x and y components and compute the angle relative to the x-axis with $project.

Sample Input Documents

Suppose you have a vectors collection with x and y components:

mongosh
[
  { "_id": ObjectId("609c26812e9274a86871bc6a"), "x": 3, "y": 4 },
  { "_id": ObjectId("609c26812e9274a86871bc6b"), "x": -2, "y": 2 },
  { "_id": ObjectId("609c26812e9274a86871bc6c"), "x": 1, "y": -1 }
]

Example 1 — Vector Angle with $atan

Calculate the angle (in radians) of each vector relative to the x-axis by dividing y by x and applying $atan:

mongosh
db.vectors.aggregate([
  {
    $project: {
      x: 1,
      y: 1,
      angleRadians: {
        $atan: { $divide: ["$y", "$x"] }
      }
    }
  }
])

How It Works

  • Document 1: atan(4/3)0.927 rad (53°).
  • Document 2: atan(2/-2)-0.785 rad (-45°).
  • Document 3: atan(-1/1)-0.785 rad (-45°).
  • Note: documents 2 and 3 share the same angle because $atan only sees the slope ratio, not the quadrant.

📈 Practical Patterns

Convert radians to degrees, use slope fields, and validate common tangent values.

Example 2 — Convert Radians to Degrees

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

mongosh
db.vectors.aggregate([
  {
    $project: {
      x: 1,
      y: 1,
      angleRadians: {
        $atan: { $divide: ["$y", "$x"] }
      },
      angleDegrees: {
        $multiply: [
          { $atan: { $divide: ["$y", "$x"] } },
          { $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 Stored Slope Field

When your documents already store a slope (rise over run), apply $atan directly:

mongosh
db.terrain.aggregate([
  {
    $project: {
      location: 1,
      slope: 1,
      inclineRadians: { $atan: "$slope" },
      inclineDegrees: {
        $multiply: [
          { $atan: "$slope" },
          { $divide: [ 180, 3.141592653589793 ] }
        ]
      }
    }
  }
])

// slope: 0   → 0° (flat)
// slope: 1   → 45°
// slope: 0.5 → ≈ 26.6°

How It Works

A slope of 1 means a 45° incline. $atan converts any stored slope ratio into the corresponding angle in radians.

Example 4 — $atan with Literal Values

Common boundary values every beginner should know:

mongosh
db.samples.aggregate([
  {
    $project: {
      atanZero:  { $atan: 0 },     // 0 rad (0°)
      atanOne:   { $atan: 1 },     // ≈ 0.785 rad (45°)
      atanNeg:   { $atan: -1 },    // ≈ -0.785 rad (-45°)
      atanLarge: { $atan: 10 }     // ≈ 1.471 rad (84.3°)
    }
  }
])

How It Works

$atan(0) = 0, $atan(1) = π/4 (45°), and large inputs approach but never reach 90°. For full quadrant angles from (x, y), see the $atan2 operator next.

🚀 Use Cases

  • Geometric computations — calculate angles from slopes in vector or coordinate data.
  • Engineering analysis — compute orientations and inclines in simulation or sensor datasets.
  • Trigonometric analysis — transform tangent ratios for signal processing and visualization.
  • Terrain and mapping — convert stored slope values into readable angle measurements.

🧠 How $atan Works

1

MongoDB reads the tangent value

The pipeline evaluates the input — a field like "$slope" or an expression like { $divide: ["$y", "$x"] }.

Input
2

$atan computes the inverse

MongoDB applies the arc tangent function and returns the angle in radians ( -π/2 to π/2 ).

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

Use the radians directly or convert to degrees for reports and dashboards.

Conclusion

The $atan operator is a practical trigonometry tool in MongoDB aggregation pipelines. It converts tangent ratios and slopes back into angles, which is essential for geometry, engineering, and any dataset that stores rise-over-run values instead of raw angles.

For beginners, remember three things: input is a slope (not an angle), output is in radians, and use $atan2 when you need the correct quadrant from separate x and y values.

💡 Best Practices

✅ Do

  • Use $atan when you have a single slope or tangent ratio
  • Remember output is in radians, not degrees
  • Use $round when displaying angle results
  • Guard against division by zero with $cond when x may be 0
  • Switch to $atan2 for full quadrant accuracy

❌ Don’t

  • Pass angle values into $atan — it expects a tangent ratio
  • Use $atan(y/x) when x can be zero without handling it
  • Assume $atan gives unique angles for all quadrants
  • Confuse $atan (inverse tangent) with $tan (tangent)
  • Use $atan as a query filter outside expressions

Key Takeaways

Knowledge Unlocked

Five things to remember about $atan

Use these points when working with trigonometric data in MongoDB.

5
Core concepts
📝 02

Simple Syntax

{ $atan: expr }

Syntax
📏 03

Any Real Input

No -1 to 1 limit.

Domain
🛠 04

Radians Output

Convert to degrees if needed.

Units
05

Use $atan2

For full quadrant angles.

Tip

❓ Frequently Asked Questions

$atan returns the arc tangent (inverse tangent) of a number. It answers: which angle has this tangent (slope) value? The result is always in radians.
The syntax is { $atan: <expression> }. The expression can be a field reference like "$slope", a literal number, or another numeric expression such as { $divide: ["$y", "$x"] }.
$atan accepts any real number — positive, negative, or zero. Unlike $asin, there is no -1 to 1 restriction.
$atan returns radians (range approximately -π/2 to π/2). To convert to degrees, multiply the result by 180 and divide by pi using $multiply and $divide.
Use $atan when you have a single slope value (y/x). Use $atan2 when you have separate x and y components and need the correct angle in all four quadrants, including when x is zero.

Continue the Math Operator Series

Move on to $atan2 for correct quadrant angles, or review $asin for arc sine calculations.

Next: $atan2 →

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