The $tan operator computes the tangent of an angle in MongoDB aggregation pipelines. Input must be in radians. Use it for slopes, trigonometric analysis, and geometry calculations alongside $sin and $cos.
01
Tangent
tan(angle).
02
Syntax
One expression.
03
Radians
Not degrees.
04
$degreesToRadians
Convert first.
05
Use Cases
Slopes, geometry.
06
vs $tanh
Trig vs hyperbolic.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $tan operator returns the tangent of an angle given in radians. For example, { $tan: 0.7853981633974483 } (45°) returns approximately 1, and { $tan: 0.5235987755982988 } (30°) returns approximately 0.5774.
Tangent represents the ratio of sine to cosine: tan(θ) = sin(θ) / cos(θ). In practice, it is useful for slope calculations, angle analysis, and engineering formulas stored in your documents.
💡
Beginner Tip
Think of $tan as MongoDB’s version of JavaScript’s Math.tan(). Both expect radians. Wrap degree fields with { $degreesToRadians: "$degrees" } first.
Foundation
📝 Syntax
The $tan operator takes one numeric expression (angle in radians):
Pass the field path directly to $tan when no degree conversion is needed.
Applications
🚀 Use Cases
Geometry and trigonometry — compute slopes, angles, and distances in spatial data.
Engineering and physics — evaluate formulas that use tangent in simulations or sensor data.
Terrain and elevation — derive incline (slope) from angle measurements.
Signal processing — combine with other trig operators for phase and waveform analysis.
🧠 How $tan Works
1
MongoDB reads the angle
The expression resolves to a number in radians — from a field, literal, or $degreesToRadians.
Input
2
$tan computes the tangent
MongoDB applies the tangent function: tan(θ) = sin(θ) / cos(θ).
Compute
3
A numeric result is returned
The tangent value is stored in the field you define in the pipeline stage.
Output
=
📐
Tangent value ready
Use for slopes, comparisons, or further math with other operators.
Wrap Up
Conclusion
The $tan operator computes the tangent of an angle in radians inside MongoDB aggregation pipelines. Convert degree fields with $degreesToRadians, and remember that tangent equals slope in basic geometry.
Do not confuse $tan with $tanh (hyperbolic tangent). Next in the series: $tanh.
Convert degrees with $degreesToRadians before calling $tan
Use $tan for slope and incline calculations from angles
Pair with $sin and $cos when building full trig pipelines
Guard null fields with $ifNull when angles may be missing
Test with known angles (0°, 45°, 60°) to verify results
❌ Don’t
Pass degree values directly to $tan without conversion
Confuse $tan (trigonometric) with $tanh (hyperbolic)
Expect defined results at 90° or 270° (cos = 0 at those angles)
Assume $tan works as a query filter outside expression stages
Forget that null input returns null
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $tan
Use these points when working with tangents in MongoDB.
5
Core concepts
📐01
Tangent
tan(θ).
Purpose
📝02
{ $tan: expr }
One argument.
Syntax
🔢03
Radians
Not degrees.
Input
🛠04
= slope
rise / run.
Geometry
⚠05
vs $tanh
Different fn.
Compare
❓ Frequently Asked Questions
$tan returns the tangent of an angle. The input must be in radians. For example, { $tan: 0.785398 } (45 degrees in radians) returns approximately 1. It is an aggregation expression operator used inside stages like $project and $addFields.
The syntax is { $tan: <expression> }. The expression can be a field reference, a literal number in radians, or a nested expression such as { $degreesToRadians: "$degrees" }.
$tan expects radians. If your data stores degrees, convert first with $degreesToRadians before passing the value to $tan.
$tan is the standard trigonometric tangent of an angle in radians. $tanh is the hyperbolic tangent of a numeric value — a different mathematical function used in machine learning and statistics.
$tan returns null when the input is null. It does not throw an error. Use $ifNull when the field may be missing.