Optional: multiply by hypotenuse to get the adjacent side of a right triangle.
Piece
Required?
Role
$cos
Yes
Cosine of a radians angle
Expression
Yes
Number in radians, field, or nested math
$degreesToRadians
No
Convert degree angles before $cos
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:
result = cos(x) // in [-1, 1]
emit { ..., field: result }
Reference
Cheat Sheet
Keep these facts handy while writing cosine expressions.
Topic
Detail
Type
Aggregation expression (not a find filter)
Syntax
{ $cos: <expression> }
Input unit
Radians
Output range
-1 to 1
Degrees pattern
{ $cos: { $degreesToRadians: <deg> } }
Triangle pattern
adjacent = cos(angle) × hypotenuse
Sample check
cos(60°) = 0.5
Try it
Live Preview
Watch $cos turn an angle into a cosine value. Degrees are converted to radians first (same idea as the examples).
Simulates $degreesToRadians then $cos (same idea as the mongosh examples below).
We use one right triangle with a 60° angle and hypotenuse 2. First we get the cosine, then we find the adjacent side. Insert the sample data once in mongosh.
Example 1 — Sample Data
Imagine one right triangle with angle 60° and hypotenuse 2. We save those as one document in a collection named trigonometry.
Same stored document. Still using 60° and hypotenuse 2 from Example 1.
Same cosine.cos(60°) ≈ 0.5 after the degrees→radians step.
Scale by length.$multiply does 0.5 × 2 ≈ 1.
Easy check. Adjacent side 1 with hypotenuse 2 matches the familiar 30-60-90 pattern.
Edge Cases & Pitfalls
Check these before using $cos in production pipelines.
units
Input is radians
Passing degrees directly gives the wrong cosine. Convert with $degreesToRadians first.
float
Floating-point noise
cos(60°) may print as 0.5000000000000001. Round when you need clean display values.
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 $acos
$cos goes angle → cosine. To go cosine → angle, use $acos.
Analysis
Performance Notes
Approach
Cost
Best for
$cos alone
Per-document math
Angles already in radians
$degreesToRadians + $cos
Still O(docs)
Human-friendly degree fields
+ $multiply
One extra product
Triangle side lengths
Trig expressions are cheap compared with large sorts or joins. Filter early with $match when only some documents need a cosine.
Remember
Key Takeaways
Role: cosine of an angle → value in [-1, 1].
Units: input must be radians.
Degrees: wrap with $degreesToRadians first.
Triangles:adjacent = cos(angle) × hypotenuse.
One line:$cos answers “what is the cosine of this angle?”—in radians in, value between -1 and 1 out.
Frequently Asked Questions
$cos returns the cosine of an angle. The input must be in radians, and the output is a number between -1 and 1. Use it inside aggregation expression stages such as $project and $addFields.
$cos expects radians. Convert degrees first with $degreesToRadians, or multiply by π/180 using $multiply and $divide.
$cos takes an angle (radians) and returns a cosine value. $acos takes a cosine value in [-1, 1] and returns an angle in radians. They are inverse operations.
$cos returns null when the input is null or the field is missing. If the argument is NaN, $cos returns NaN.
adjacent = cos(angle) × hypotenuse. Convert the angle to radians, pass it to $cos, then multiply by the hypotenuse.
Yes for the math. Think of $cos as MongoDB’s aggregation version of Math.cos—run it in the pipeline, not as a standalone find() filter.