$cos (Cosine)

Last updated: 2026-10-09

$cos (Cosine)

$cos returns the cosine of a value expressed in radians. It is used for vector projection, waveform analysis, and rotation math. A null or missing argument yields null, NaN input returns NaN, and ±Infinity throws an error. The cosine range is -1 to 1, and $acos provides the inverse function. Convert between degrees and radians with $degreesToRadians and $radiansToDegrees rather than hand-multiplying by pi.

CategoryMath
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

{ $cos: <numeric-expression> }

⚙️ Parameters

radiansA numeric expression in radians.

Returns:The cosine 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):

// $cos: cosine after converting degrees to radians
db.trig.aggregate([
  { $project: {
      x: { $cos: { $degreesToRadians: '$deg' } }
  } }
])

Output:

{ '_id': 1, 'x': 0.5 }
{ '_id': 2, 'x': 0.8660254037844387 }

📌 Notes

More hands-on cases coming soon.

📚 Related Tutorials

❓ FAQ

QWhat unit does $cos expect?
ARadians; convert angles with $degreesToRadians first.
QWhat is the range of $cos results?
AResults fall between -1 and 1.
QHow are $cos and $sin related?
AFor the same angle, the squares of $cos and $sin sum to 1, so they can be converted into each other.
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%

🙏 帮我们做得更好

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

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