MongoDB $tan Operator

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

What Is $tan?

$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
UsePrefer
Tangent of an angle$tan
Degrees → radians$degreesToRadians
Angle from a tangent value$atan

How to Use $tan in MongoDB

Start from radians (or convert degrees), then take tangent. Multiply by a length when you need a side.

  1. Choose an expression stage ($addFields or $project).
  2. If the angle is in degrees, convert with $degreesToRadians.
  3. Pass that radians expression to $tan.
  4. Optional: multiply by the adjacent side to get the opposite side of a right triangle.
PieceRequired?Role
$tanYesTangent of a radians angle
ExpressionYesNumber in radians, field, or nested math
$degreesToRadiansNoConvert 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 }

Cheat Sheet

Keep these facts handy while writing tangent expressions.

TopicDetail
TypeAggregation expression (not a find filter)
Syntax{ $tan: <expression> }
Input unitRadians
Degrees pattern{ $tan: { $degreesToRadians: <deg> } }
Triangle patternopposite = tan(angle) × adjacent
Sample checktan(45°) = 1
AvoidOdd multiples of 90° (tangent undefined)

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).

Result
angle = 45 °
radians ≈ 0.785398
$tan → 1

{ tangent: 1 }

Worked Walkthrough — 45° Triangle Side

Right triangle with adjacent side 3 and angle A = 45°. Opposite side = tan(A) × adjacent.

Calculation
Document:
  { angle_deg: 45, side_a: 3 }

radians = degreesToRadians(45)
        ≈ 0.7854

tan(45°) = 1

opposite = tan(A) × adjacent
         = 1 × 3
         = 3

Check: at 45°, opposite equals adjacent ✓

MongoDB Examples

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.

mongosh
db.trigonometry.insertOne({
  _id: 1,
  angle_deg: 45,
  side_a: 3
});

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)
angle_deg45Angle A in degrees (not radians yet)
side_a3Adjacent side (next to angle A)
Picture
Right triangle (easy 45-45-90 style numbers):

        |\
        | \
  opp   |  \
        |   \
        |    \
        |_____\
          3
        angle A = 45°
        (adjacent = side_a)

  tan(A) = opposite / adjacent
  opposite = tan(A) × adjacent

How It Works

  1. Insert once. insertOne adds a single triangle document to trigonometry.
  2. MongoDB stores fields as written. angle_deg is 45 (degrees); side_a is 3.
  3. find() shows the same shape. What you inserted is what you get back—ready for $tan in the next examples.

Example 2 — Tangent Value

Goal: convert 45° to radians, then take $tan to get the tangent (should be 1).

mongosh
db.trigonometry.aggregate([
  {
    $addFields: {
      tangent: {
        $tan: {
          $degreesToRadians: "$angle_deg"
        }
      }
    }
  }
]);

How It Works

  1. Read the stored angle. MongoDB already has angle_deg: 45 from Example 1.
  2. Convert units. $degreesToRadians turns 45° into about 0.785 radians.
  3. Take tangent. $tan returns about 1 (floating-point may show 0.9999999999999999).
  4. Keep all fields. $addFields adds tangent next to the original angle and side.

Example 3 — Opposite Side

Same triangle: multiply tangent by the adjacent side to get the opposite side length (should be 3).

mongosh
db.trigonometry.aggregate([
  {
    $addFields: {
      opposite: {
        $multiply: [
          {
            $tan: {
              $degreesToRadians: "$angle_deg"
            }
          },
          "$side_a"
        ]
      }
    }
  }
]);

How It Works

  1. Same stored document. Still using 45° and adjacent side 3 from Example 1.
  2. Same tangent. tan(45°) ≈ 1 after the degrees→radians step.
  3. Scale by length. $multiply does 1 × 3 ≈ 3.
  4. 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.

Performance Notes

ApproachCostBest for
$tan alonePer-document mathAngles already in radians
$degreesToRadians + $tanStill O(docs)Human-friendly degree fields
+ $multiplyOne extra productTriangle side lengths

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

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.

Next: $tanh

Compute the hyperbolic tangent in an aggregation pipeline.

$tanh 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