$tan (Tangent)
Last updated: 2026-10-09
$tan (Tangent)
$tan returns the tangent of a value expressed in radians. It is used for slope calculations, gradient conversion, and angle measurement. A null or missing argument yields null, NaN input returns NaN, and ±Infinity throws an error. Note that tangent is undefined at odd multiples of 90 degrees (π/2 radians), where the result may be extremely large or null. Convert angles with $degreesToRadians first.
| Category | Math |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $tan: <numeric-expression> }
⚙️ Parameters
| radians | A numeric expression in radians. |
|---|
Returns:The tangent of the input radians; null when null or missing, error for ±Infinity
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $tan: tangent after converting degrees to radians
db.trig.aggregate([
{ $project: {
slope: { $tan: { $degreesToRadians: '$deg' } }
} }
])
Output:
{ '_id': 1, 'slope': 0.9999999999999999 }
{ '_id': 2, 'slope': 1.7320508075688767 }
📌 Notes
- Tangent is undefined at odd multiples of 90 degrees, where results may be odd
- The input is in radians
- $atan is the inverse and also returns radians
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QHow do $tan, $sin, and $cos relate?
A$tan equals $sin divided by $cos, so the tangent is the ratio of sine to cosine.
QWhat does $tan return for 90 degrees?
ATangent is undefined at 90 degrees and the result is unreliable; check the angle first.
QHow do I recover an angle from $tan?
AApply $atan to the tangent to get the inverse, then convert the radians to degrees.