$cos
New from version 8.0.1.
The $cos operator in Amazon DocumentDB returns the cosine of a value that is measured in radians. Use it in the aggregation pipeline to perform trigonometric calculations on numeric fields.
Parameters
-
expression: An expression that resolves to a number in radians.
Example (MongoDB Shell)
The following example shows how to use the $cos operator to calculate the cosine of angle values in radians.
Create sample documents
db.angles.insertMany([ { "_id": 1, "angle": 0 }, { "_id": 2, "angle": 1.0471975511965976 }, { "_id": 3, "angle": 3.141592653589793 } ]);
Query example
db.angles.aggregate([ { $project: { "cosine": { $cos: "$angle" } }} ]);
Output
[
{ "_id": 1, "cosine": 1 },
{ "_id": 2, "cosine": 0.5000000000000001 },
{ "_id": 3, "cosine": -1 }
]
Code examples
To view a code example for using the $cos operator, choose the tab for the language that you want to use: