MongoDB $tan Operator

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 4 Examples
Trigonometry

What You’ll Learn

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.

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.

📝 Syntax

The $tan operator takes one numeric expression (angle in radians):

mongosh
{ $tan: <expression> }

Literal Examples

mongosh
{ $tan: 0 }
// Result: 0

{ $tan: 0.7853981633974483 }
// ~45° in radians → ~1

{ $tan: { $degreesToRadians: 30 } }
// 30° → ~0.5774

Syntax Rules

  • $tan — returns the tangent of the angle that follows.
  • <expression> — angle in radians (field path, literal, or nested expression).
  • Use inside $project, $addFields, or $set.
  • Convert degrees with $degreesToRadians before calling $tan.
  • Undefined at odd multiples of π/2 (e.g., 90°, 270°) — may return very large values or null depending on input.
  • null input — returns null.

💡 $tan vs $tanh vs $sin / $cos

$tan — trigonometric tangent of an angle (radians)
$tanh — hyperbolic tangent (different function; used in ML/stats)
$sin / $cos — related trig functions; tan = sin / cos
Do not confuse $tan with $tanh — they solve different problems.

⚡ Quick Reference

QuestionAnswer
Operator typeAggregation expression operator (trigonometry)
Syntax{ $tan: <expression> }
Input unitRadians (not degrees)
Degree conversion{ $degreesToRadians: "$degrees" }
Common valuestan(0°) = 0, tan(45°) = 1, tan(60°) ≈ 1.732
Common stages$project, $addFields, $set
Radians
{
  $tan: "$radians"
}

Field in radians

From degrees
{
  $tan: {
    $degreesToRadians: "$deg"
  }
}

Convert first

45°
{
  $tan: 0.785398
}

Returns ~1

sin / cos
$divide: [
  { $sin: "$r" },
  { $cos: "$r" }
]

Equivalent tan

Examples Gallery

Compute tangents from degree fields, work with radian literals, and derive slope values from angles.

📚 Tangent from Degrees

Convert degree fields to radians, then apply $tan in a $project stage.

Sample Input Documents

Suppose you have an angles collection with a degrees field:

mongosh
[
  { "_id": 1, "label": "30° angle",  "degrees": 30 },
  { "_id": 2, "label": "45° angle",  "degrees": 45 },
  { "_id": 3, "label": "60° angle",  "degrees": 60 }
]

Example 1 — $tan with $degreesToRadians

Compute the tangent of each angle stored in degrees:

mongosh
db.angles.aggregate([
  {
    $project: {
      label: 1,
      degrees: 1,
      tangent: {
        $tan: {
          $degreesToRadians: "$degrees"
        }
      }
    }
  }
])

How It Works

  • $degreesToRadians converts 30° to about 0.5236 radians.
  • $tan then returns tan(0.5236) ≈ 0.5774.
  • 45° gives tan ≈ 1; 60° gives tan ≈ √3 ≈ 1.7321.

📈 Radians, Slopes, and Equivalence

Apply $tan to radian fields and relate it to sine and cosine.

Example 2 — Tangent of a Radian Literal

When the angle is already in radians, pass it directly:

mongosh
db.angles.aggregate([
  {
    $project: {
      tangentZero: { $tan: 0 },
      tangentPi4:  { $tan: 0.7853981633974483 }
    }
  }
])

// $tan(0)           → 0
// $tan(π/4 radians) → ~1

How It Works

Skip $degreesToRadians when your data is already stored in radians.

Example 3 — Slope from an Angle

Tangent of an angle equals rise over run (slope) for a line:

mongosh
db.terrain.aggregate([
  {
    $project: {
      segment: 1,
      angleDegrees: 1,
      slope: {
        $tan: {
          $degreesToRadians: "$angleDegrees"
        }
      }
    }
  }
])

// angleDegrees: 45 → slope: ~1 (45° incline)
// angleDegrees: 0  → slope: 0 (flat)

How It Works

In geometry, slope = tan(θ). A 45° angle has a slope of 1 (rise equals run).

Example 4 — tan as sin / cos

Verify that tangent equals sine divided by cosine:

mongosh
db.angles.aggregate([
  {
    $project: {
      degrees: 1,
      viaTan: {
        $tan: {
          $degreesToRadians: "$degrees"
        }
      },
      viaSinCos: {
        $divide: [
          {
            $sin: {
              $degreesToRadians: "$degrees"
            }
          },
          {
            $cos: {
              $degreesToRadians: "$degrees"
            }
          }
        ]
      }
    }
  }
])

How It Works

Both expressions produce the same result. Use $tan for clarity when you only need the tangent.

Bonus — $tan on a Radians Field

When documents already store angles in radians:

mongosh
db.measurements.aggregate([
  {
    $project: {
      sensor: 1,
      radians: 1,
      tangent: { $tan: "$radians" }
    }
  }
])

How It Works

Pass the field path directly to $tan when no degree conversion is needed.

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

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.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about $tan

Use these points when working with tangents in MongoDB.

5
Core concepts
📝 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.

Continue the Operator Series

Move on to $tanh for hyperbolic tangent, or review $sin for sine calculations.

Next: $tanh →

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.

5 people found this page helpful