$tan is a MongoDB aggregation expression that returns the tangent of an angle. The angle must be in radians. In a right triangle, tan(angle) = opposite / adjacent.
Remember
$tan = Math.tan for aggregation
{ $tan: <expression> }
input → radians
output → tangent (number)
degrees → wrap with $degreesToRadians first
null / missing → null
NaN → NaN
±Infinity → pipeline error
Optional: multiply by the adjacent side to get the opposite side of a right triangle.
Piece
Required?
Role
$tan
Yes
Tangent of a radians angle
Expression
Yes
Number in radians, field, or nested math
$degreesToRadians
No
Convert degree angles before $tan
Pseudocode
Pseudocode
for each document:
x = evaluate(expression) // radians
if x is null or missing:
result = null
else if x is NaN:
result = NaN
else if x is ±Infinity:
error // aggregation fails
else:
result = tan(x)
emit { ..., field: result }
Reference
Cheat Sheet
Keep these facts handy while writing tangent expressions.
Topic
Detail
Type
Aggregation expression (not a find filter)
Syntax
{ $tan: <expression> }
Input unit
Radians
Degrees pattern
{ $tan: { $degreesToRadians: <deg> } }
Triangle pattern
opposite = tan(angle) × adjacent
Sample check
tan(45°) = 1
Avoid
Odd multiples of 90° (tangent undefined)
Try it
Live Preview
Watch $tan turn an angle into a tangent value. Degrees are converted to radians first (same idea as the examples).
Simulates $degreesToRadians then $tan (same idea as the mongosh examples below).
We use one right triangle with a 45° angle and adjacent side 3. First we get the tangent, then we find the opposite side. Insert the sample data once in mongosh.
Example 1 — Sample Data
Imagine one right triangle with angle 45° and adjacent side 3. We save those as one document in a collection named trigonometry.
Same stored document. Still using 45° and adjacent side 3 from Example 1.
Same tangent.tan(45°) ≈ 1 after the degrees→radians step.
Scale by length.$multiply does 1 × 3 ≈ 3.
Easy check. At 45°, opposite equals adjacent—both are 3.
Edge Cases & Pitfalls
Check these before using $tan in production pipelines.
units
Input is radians
Passing degrees directly gives the wrong tangent. Convert with $degreesToRadians first.
domain
Avoid 90° (and ±Infinity)
Tangent is undefined at odd multiples of 90°. $tan on ±Infinity throws a pipeline error.
null
Null and missing become null
Missing fields return null, not an error. Handle them before trig if nulls are invalid in your data.
inverse
Need the angle? Use $atan
$tan goes angle → tangent. To go tangent → angle, use $atan.
Analysis
Performance Notes
Approach
Cost
Best for
$tan alone
Per-document math
Angles already in radians
$degreesToRadians + $tan
Still O(docs)
Human-friendly degree fields
+ $multiply
One extra product
Triangle side lengths
Trig expressions are cheap compared with large sorts or joins. Filter early with $match when only some documents need a tangent.
Remember
Key Takeaways
Role: tangent of an angle → opposite / adjacent ratio.
Units: input must be radians.
Degrees: wrap with $degreesToRadians first.
Triangles:opposite = tan(angle) × adjacent.
One line:$tan answers “what is the tangent of this angle?”—radians in, ratio out.
Frequently Asked Questions
$tan returns the tangent of an angle. The input must be in radians. Use it inside aggregation expression stages such as $project and $addFields.
$tan expects radians. Convert degrees first with $degreesToRadians, or multiply by π/180 using $multiply and $divide.
$tan is the trigonometric tangent of an angle in radians. $tanh is the hyperbolic tangent of a number—a different function used in statistics and machine learning.
$tan returns null when the input is null or the field is missing. If the argument is NaN, $tan returns NaN. Positive or negative infinity causes an error.
opposite = tan(angle) × adjacent. Convert the angle to radians, pass it to $tan, then multiply by the adjacent side.
Yes for the math. Think of $tan as MongoDB’s aggregation version of Math.tan—run it in the pipeline, not as a standalone find() filter.