MongoDB $atan Operator

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

What Is $atan?

$atan is a MongoDB aggregation expression that returns the inverse tangent (arc tangent) of a number—the angle whose tangent equals that value. Results are in radians (about -π/2 to π/2). Input can be any real number.

Remember
$atan = Math.atan for aggregation

{ $atan: <expression> }

input  → any real number (slope / tangent value)
output → radians (−π/2 … π/2)
degrees → wrap with $radiansToDegrees

null / missing → null
NaN → NaN
UsePrefer
Angle from a tangent / slope$atan
Separate x, y (all quadrants)$atan2
Radians → degrees$radiansToDegrees

How to Use $atan in MongoDB

Pass a tangent value (or a slope ratio), then convert units if needed.

  1. Choose an expression stage ($addFields or $project).
  2. Build the tangent if needed (often opposite / adjacent via $divide).
  3. Pass that expression to $atan to get radians.
  4. Optionally wrap with $radiansToDegrees for a degree result.
PieceRequired?Role
$atanYesInverse tangent → radians
ExpressionYesAny real number, field, or nested math
$radiansToDegreesNoConvert output when you want degrees

Pseudocode

Pseudocode
for each document:
    x = evaluate(expression)
    if x is null or missing:
        result = null
    else if x is NaN:
        result = NaN
    else:
        result = atan(x)   // radians, any real x
    emit { ..., field: result }

Cheat Sheet

Keep these facts handy while writing inverse-tangent expressions.

TopicDetail
TypeAggregation expression (not a find filter)
Syntax{ $atan: <expression> }
DomainAll real numbers
RangeAbout -π/2 to π/2 radians
Degrees pattern{ $radiansToDegrees: { $atan: <x> } }
Triangle pattern$atan({ $divide: [opposite, adjacent] })
Sample check3-4-5 triangle: atan(3/4) ≈ 36.87°

Live Preview

Watch $atan turn a tangent value into radians (and degrees). Negatives and zero are valid.

Simulates $atan then $radiansToDegrees (same idea as the mongosh examples below).

Result
x = 0.75
$atan(x) → 0.643501 rad
$radiansToDegrees → 36.869898 °

{ angle_rad: 0.643501, angle_deg: 36.869898 }

Worked Walkthrough — 3-4-5 Triangle

Right triangle with sides 3, 4, and 5. Angle A is opposite side_a (3), so tangent of A = opposite / adjacent = 3 / 4.

Calculation
Document:
  { side_a: 3, side_b: 4, hypotenuse: 5 }

tan(A) = opposite / adjacent
       = side_a / side_b
       = 3 / 4
       = 0.75

$atan(0.75) ≈ 0.6435 radians

$radiansToDegrees(...) ≈ 36.87 degrees

Check: tan(36.87°) ≈ 0.75 ✓

MongoDB Examples

We use a 3-4-5 right triangle. First we get the angle in radians, then convert to degrees. Run these in mongosh after inserting the sample data once.

Example 1 — Sample Data

Imagine one right triangle with sides 3, 4, and 5. We save those three lengths as one document in a collection named trigonometry.

mongosh
db.trigonometry.insertOne({
  _id: 1,
  side_a: 3,
  side_b: 4,
  hypotenuse: 5
});

How the Data Is Stored in MongoDB

After the insert, MongoDB keeps one document in trigonometry. Run find() to see it:

mongosh
db.trigonometry.find()

Read that document like this:

Field in MongoDBValuePlain English
_id1Document id (row number for this lesson)
side_a3Opposite side of angle A
side_b4Adjacent side of angle A
hypotenuse5Longest side (not needed for $atan)
Picture
Right triangle (easy 3-4-5):

        |\
        | \
  3     |  \   5  (hypotenuse)
(side_a)|   \
        |    \
        |_____\
           4
        (side_b)

We will find angle A (at the bottom-left corner):
  tan(A) = side opposite A / side next to A
         = side_a / side_b
         = 3 / 4
         = 0.75

How It Works

  1. Insert once. insertOne adds a single triangle document to trigonometry.
  2. MongoDB stores fields as written. side_a, side_b, and hypotenuse are just numbers: 3, 4, and 5.
  3. find() shows the same shape. What you inserted is what you get back—ready for $atan in the next examples.

Example 2 — Angle in Radians

Goal: compute angle A in radians using tan(A) = side_a / side_b.

mongosh
db.trigonometry.aggregate([
  {
    $addFields: {
      angle_a_rad: {
        $atan: {
          $divide: ["$side_a", "$side_b"]
        }
      }
    }
  }
]);

How It Works

  1. Read the stored sides. MongoDB already has side_a: 3 and side_b: 4 from Example 1.
  2. Build the tangent. $divide: ["$side_a", "$side_b"] → 3 / 4 = 0.75.
  3. Take inverse tangent. $atan(0.75) returns the angle in radians (about 0.6435).
  4. Keep all fields. $addFields adds angle_a_rad next to the original sides—nothing is deleted.

Example 3 — Angle in Degrees

Same triangle, but wrap $atan with $radiansToDegrees for a more familiar unit.

mongosh
db.trigonometry.aggregate([
  {
    $addFields: {
      angle_a: {
        $radiansToDegrees: {
          $atan: {
            $divide: ["$side_a", "$side_b"]
          }
        }
      }
    }
  }
]);

How It Works

  1. Same stored document. Still using sides 3, 4, and 5 from Example 1.
  2. Same tangent ratio. 3 / 4 = 0.75, then $atan gives radians (~0.6435).
  3. Convert units. $radiansToDegrees turns that into about 36.87°.
  4. Easy check. In a 3-4-5 triangle, the smaller acute angle is about 37°—so 36.87° looks right.

Edge Cases & Pitfalls

Check these before using $atan in production pipelines.

units

Output is radians

Do not treat the raw result as degrees. Convert with $radiansToDegrees when your report expects °.

quad

Need all four quadrants?

A single slope loses quadrant info. Prefer $atan2 when you have separate x and y (especially if x can be zero).

null

Null and missing become null

Missing fields return null, not an error. Handle them before trig if nulls are invalid in your data.

div

Avoid divide-by-zero in slopes

Triangle or slope ratios need a non-zero adjacent/x. Guard with $cond or switch to $atan2.

Performance Notes

ApproachCostBest for
$atan alonePer-document mathAngles already stored as tangents / slopes
$divide + $atanStill O(docs)Right-triangle angles from sides
+ $radiansToDegreesOne extra conversionHuman-readable degree reports

Trig expressions are cheap compared with large sorts or joins. Filter early with $match when only some documents need an angle.

Key Takeaways

  • Role: inverse tangent → angle in radians.
  • Domain: any real number (no [-1, 1] clamp).
  • Degrees: wrap with $radiansToDegrees.
  • Triangles: $atan(opposite / adjacent).

One line: $atan answers “which angle has this tangent?”—in radians, for any real slope.

Frequently Asked Questions

$atan returns the inverse tangent (arc tangent) of a number—the angle whose tangent is that number. Results are in radians, roughly from -π/2 to π/2. Use it inside aggregation expression stages such as $project and $addFields.
Any real number—positive, negative, or zero. Unlike $asin or $acos, there is no [-1, 1] domain clamp.
$atan returns radians. Convert with $radiansToDegrees, or multiply by 180/π using $multiply and $divide.
$atan returns null when the input is null or the field is missing. If the argument is NaN, $atan returns NaN.
Use $atan when you already have a single slope (or y/x). Use $atan2 when you have separate x and y and need the correct quadrant, including when x is zero.
Yes for the math. Think of $atan as MongoDB’s aggregation version of Math.atan—run it in the pipeline, not as a standalone find() filter.

Next: $atan2

Compute a two-argument inverse tangent with correct quadrant handling.

$atan2 tutorial →

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.

9 people found this page helpful