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

CategoryMath
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

{ $tan: <numeric-expression> }

⚙️ Parameters

radiansA 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

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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏