Sample Input Documents
Suppose you have a data collection with numeric value fields:
[
{ "_id": 1, "value": 1 },
{ "_id": 2, "value": 0.5 },
{ "_id": 3, "value": -1 },
{ "_id": 4, "value": 0 }
] 
The $tanh operator computes the hyperbolic tangent of a number in MongoDB aggregation pipelines. Output is always between -1 and 1, making it useful for normalization, sigmoid-style transforms, and ML-related scoring.
tanh(x).
One expression.
-1 to 1 output.
Transform fields.
ML, normalization.
Hyperbolic vs trig.
In MongoDB’s aggregation framework, the $tanh operator returns the hyperbolic tangent of a numeric expression. Mathematically, tanh(x) = sinh(x) / cosh(x). For example, { $tanh: 0 } returns 0, and { $tanh: 1 } returns approximately 0.761594.
Unlike trigonometric $tan, hyperbolic tangent takes any real number (not an angle in radians) and always produces a value strictly between -1 and 1. Large positive inputs approach 1; large negative inputs approach -1.
Think of $tanh as MongoDB’s version of JavaScript’s Math.tanh(). It is an odd function: tanh(-x) = -tanh(x).
The $tanh operator takes one numeric expression:
{ $tanh: <expression> } { $tanh: 0 }
// Result: 0
{ $tanh: 1 }
// Result: ~0.761594
{ $tanh: -1 }
// Result: ~-0.761594
{ $tanh: "$value" }
// Hyperbolic tangent of the value field $tanh — returns the hyperbolic tangent of the expression.<expression> — any real number (field path, literal, or nested expression).-1 and 1 (exclusive at extremes).tanh(0) — always 0.$project, $addFields, or $set.null input — returns null.$tanh when you need the bounded ratio.| Question | Answer |
|---|---|
| Operator type | Aggregation expression operator (hyperbolic) |
| Syntax | { $tanh: <expression> } |
| Input | Any real number |
| Output range | Between -1 and 1 |
| tanh(0) | 0 |
| Common stages | $project, $addFields, $set |
{
$tanh: "$value"
}On a field
{
$tanh: 0
}Returns 0
{
$tanh: 10
}Approaches 1
tanh(-x)
= -tanh(x)Sign flip
Compute hyperbolic tangents on document fields, verify odd-function behavior, and apply sigmoid-style normalization.
Start with a data collection and apply $tanh with $project.
Suppose you have a data collection with numeric value fields:
[
{ "_id": 1, "value": 1 },
{ "_id": 2, "value": 0.5 },
{ "_id": 3, "value": -1 },
{ "_id": 4, "value": 0 }
] Compute the hyperbolic tangent of each value:
db.data.aggregate([
{
$project: {
value: 1,
tanhValue: { $tanh: "$value" }
}
}
]) See how large values saturate and compare with related operators.
Verify that tanh(-x) equals -tanh(x):
db.data.aggregate([
{
$project: {
positive: { $tanh: 2 },
negative: { $tanh: -2 }
}
}
])
// positive: ~0.964028
// negative: ~-0.964028 Hyperbolic tangent is an odd function. Negative inputs produce the negated result of the positive counterpart.
Large inputs approach but never exceed 1 or -1:
db.data.aggregate([
{
$project: {
small: { $tanh: 0.5 },
medium: { $tanh: 2 },
large: { $tanh: 10 }
}
}
])
// $tanh(0.5) → ~0.462
// $tanh(2) → ~0.964
// $tanh(10) → ~0.9999999959 (approaches 1) This saturation property makes $tanh useful for squashing unbounded scores into a fixed range.
Transform raw model scores into a bounded activation range:
db.predictions.aggregate([
{
$project: {
modelId: 1,
rawScore: 1,
activation: { $tanh: "$rawScore" }
}
}
])
// rawScore: 3.5 → activation: ~0.998
// rawScore: 0 → activation: 0
// rawScore: -2.1 → activation: ~-0.97 In machine learning pipelines, tanh acts like a sigmoid activation, mapping any real score into (-1, 1) for downstream processing.
Mathematically, tanh equals sinh divided by cosh:
db.data.aggregate([
{
$project: {
value: 1,
viaTanh: { $tanh: "$value" },
viaRatio: {
$divide: [
{ $sinh: "$value" },
{ $cosh: "$value" }
]
}
}
}
]) Both expressions produce the same result. Use $tanh directly for clarity and performance.
$tanh WorksThe input resolves to a real number from a field path, literal, or nested expression.
MongoDB evaluates tanh(x) = (ex - e-x) / (ex + e-x).
The output is always between -1 and 1, stored in your pipeline field.
Use for activations, bounded scores, or further math in the pipeline.
The $tanh operator computes hyperbolic tangent in MongoDB aggregation pipelines, producing values strictly between -1 and 1. It is essential for sigmoid-style normalization, ML scoring, and any task that needs to bound unbounded numeric data.
Do not confuse it with trigonometric $tan, which expects radians and is unbounded. Next in the series: $text.
$tanh to squash unbounded scores into (-1, 1)$sinh and $cosh when building custom math$ifNull before applying $tanh$tanh (hyperbolic) with $tan (trigonometric)$tanh expecting trig behavior$tanh works as a query filter outside expressionsnull input returns null$tanhUse these points when applying hyperbolic tangent in MongoDB.
tanh(x).
PurposeOne argument.
SyntaxBounded.
OutputML activation.
Use caseNot trig.
CompareMove on to $text for full-text search queries, or review $sinh for hyperbolic sine.
5 people found this page helpful