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.
Fundamentals
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.
Foundation
📝 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
Cheat Sheet
⚡ Quick Reference
Question
Answer
Operator type
Aggregation expression operator (trigonometry)
Syntax
{ $atan: <expression> }
Input range
Any real number
Output unit
Radians (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
Hands-On
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:
$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.
Applications
🚀 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.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $atan
Use these points when working with trigonometric data in MongoDB.
5
Core concepts
📐01
Inverse Tangent
Slope ratio → angle in radians.
Purpose
📝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.