MongoDB $asinh

In MongoDB, the $asinh aggregation pipeline operator returns the hyperbolic arcsine (inverse hyperbolic sine) of a value, measured in radians.

$asinh accepts any valid expression that resolves to a number.

The $asinh operator was introduced in MongoDB 4.2.

Example

Suppose we have a collection called test with the following document:

{ "_id" : 1, "data" : 180 }

We can use the $asinh operator to return the hyperbolic arcsine of the data field:

db.test.aggregate(
  [
    { $match: { _id: 1 } },
    { $project: { 
        _id: 0,
        hyperbolicArcsine: { $asinh: "$data" }
      }
    }
  ]
)

Result:

{ "hyperbolicArcsine" : 5.886111747410234 }

By default, the $asinh 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.

Convert to Degrees

As mentioned, $asinh returns its result in radians. You can use the $radiansToDegrees operator if you want the result in degrees.

Example:

db.test.aggregate(
  [
    { $match: { _id: 1 } },
    { $project: { 
        _id: 0,
        radians: { $asinh: "$data" },
        degrees: { $radiansToDegrees: { $asinh: "$data" } }
      }
    }
  ]
)

Result:

{ "radians" : 5.886111747410234, "degrees" : 337.2493608689805 }

In this example, the first field presents the result in radians, and the second field presents it in degrees.

128-Bit Decimal Values

If the expression provided to $asinh is 128-bit decimal, then the result is returned in 128-bit decimal.

Suppose we add the following document to our collection:

{ "_id" : 2, "data" : NumberDecimal("90.1301023541559787031443874490659") }

Let’s run the the $asinh operator against that document:

db.test.aggregate(
  [
    { $match: { _id: 2 } },
    { $project: { 
        _id: 0,
        hyperbolicArcsine: { $asinh: "$data" }
      }
    }
  ]
)

Result:

{ "hyperbolicArcsine" : NumberDecimal("5.194432162492309602580058740782701") }

The output is 128-bit decimal.

Null Values

Null values return null when using the $asinh operator.

Suppose we add the following document to our collection:

{ "_id" : 3, "data" : null }

Let’s run the the $asinh operator against that document:

db.test.aggregate(
  [
    { $match: { _id: 3 } },
    { $project: { 
        _id: 0,
        result: { $asinh: "$data" }
      }
    }
  ]
)

Result:

{ "result" : null }

We can see that the result is null.

NaN Values

If the argument resolves to NaN$asinh returns NaN.

Example:

db.test.aggregate(
  [
    { $match: { _id: 3 } },
    { $project: { 
        _id: 0,
        result: { $asinh: 1 * "$data" }
      }
    }
  ]
)

Result:

{ "result" : NaN }

Infinity

If the argument resolves to Infinity or -Infinity, the $asinh operator returns Infinity.

Suppose we add the following document to our collection:

{ "_id" : 4, "data" : Infinity }

Let’s run $asinh against the data field:

db.test.aggregate(
  [
    { $match: { _id: 4 } },
    { $project: { 
        _id: 0,
        hyperbolicArcsine: { $asinh: "$data" }
      }
    }
  ]
)

Result:

{ "hyperbolicArcsine" : Infinity }

Non-Existent Fields

If the $asinh operator is applied against a field that doesn’t exist, null is returned.

Example:

db.test.aggregate(
  [
    { $match: { _id: 4 } },
    { $project: { 
        _id: 0,
        result: { $asinh: "$wrong" }
      }
    }
  ]
)

Result:

{ "result" : null }