In MongoDB, the $tan
aggregation pipeline operator returns the tangent of a value that is measured in radians.
$tan
accepts any valid expression that resolves to a number.
The $tan
operator was introduced in MongoDB 4.2.
Example
Suppose we have a collection called test
with the following document:
{ "_id" : 1, "data" : 2 }
We can use the $tan
operator to return the tangent of the data
field:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
tangent: { $tan: "$data" }
}
}
]
)
Result:
{ "tangent" : -2.185039863261519 }
Convert to Radians
As mentioned, $tan
returns the tangent of a value that is measured in radians. If the value is in degrees, you can use the $degreesToRadians
operator to convert it to radians.
Example:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
tangent: { $degreesToRadians: { $tan: "$data" } }
}
}
]
)
Result:
{ "tangent" : -0.038136139901240186 }
128-Bit Decimal Values
By default, the $tan
operator returns values as a double
, but it can also return values as a 128-bit decimal as long as the expression resolves to a 128-bit decimal value.
Suppose we add the following document to our collection:
{ "_id" : 2, "data" : NumberDecimal("2.1301023541559787031443874490659") }
Let’s run the the $tan
operator against that document:
db.test.aggregate(
[
{ $match: { _id: 2 } },
{ $project: {
_id: 0,
tangent: { $tan: "$data" }
}
}
]
)
Result:
{ "tangent" : NumberDecimal("-1.597486946407534452195921861435753") }
The output is 128-bit decimal.
Null Values
Null values return null
when using the $tan
operator.
Suppose we add the following document to our collection:
{ "_id" : 3, "data" : null }
Let’s run the the $tan
operator against that document:
db.test.aggregate(
[
{ $match: { _id: 3 } },
{ $project: {
_id: 0,
result: { $tan: "$data" }
}
}
]
)
Result:
{ "result" : null }
We can see that the result is null
.
NaN Values
If the argument resolves to NaN
, $tan
returns NaN
.
Example:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
result: { $tan: 1 * "$data" }
}
}
]
)
Result:
{ "result" : NaN }
Non-Existent Fields
If the $tan
operator is applied against a field that doesn’t exist, null
is returned.
Example:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
result: { $tan: "$name" }
}
}
]
)
Result:
{ "result" : null }
Infinity
Providing Infinity
or -Infinity
returns an error.
Suppose we add the following documents to the collection:
{ "_id" : 4, "data" : Infinity } { "_id" : 5, "data" : -Infinity }
Let’s apply $tan
to these documents:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 4, 5 ] } } },
{ $project: {
tangent: { $tan: "$data" }
}
}
]
)
Result:
uncaught exception: Error: command failed: { "ok" : 0, "errmsg" : "cannot apply $tan to inf, value must in (-inf,inf)", "code" : 50989, "codeName" : "Location50989" } : aggregate failed : _getErrorWithCode@src/mongo/shell/utils.js:25:13 doassert@src/mongo/shell/assert.js:18:14 _assertCommandWorked@src/mongo/shell/assert.js:618:17 assert.commandWorked@src/mongo/shell/assert.js:708:16 DB.prototype._runAggregate@src/mongo/shell/db.js:266:5 DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1046:12 @(shell):1:1
In this case, $tan
only got as far as the first document (document 4) before an error occurred. We know this because the error message indicates that it was trying to apply $tan
to inf (Infinity
).
If we remove document 4 from the query filter, we can see that it progresses to document 5 and returns the same error.
db.test.aggregate(
[
{ $match: { _id: { $in: [ 5 ] } } },
{ $project: {
tangent: { $tan: "$data" }
}
}
]
)
Result:
uncaught exception: Error: command failed: { "ok" : 0, "errmsg" : "cannot apply $tan to -inf, value must in (-inf,inf)", "code" : 50989, "codeName" : "Location50989" } : aggregate failed : _getErrorWithCode@src/mongo/shell/utils.js:25:13 doassert@src/mongo/shell/assert.js:18:14 _assertCommandWorked@src/mongo/shell/assert.js:618:17 assert.commandWorked@src/mongo/shell/assert.js:708:16 DB.prototype._runAggregate@src/mongo/shell/db.js:266:5 DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1046:12 @(shell):1:1
This time the error shows that it was trying to apply $tan
to -inf (-Infinity
).