In MongoDB, the $asin aggregation pipeline operator returns the arcsine (inverse sine) of a value, measured in radians.
$asin accepts any valid expression that resolves to a number between -1 and 1.
The $asin operator was introduced in MongoDB 4.2.
Example
Suppose we have a collection called test with the following document:
{ "_id" : 1, "data" : 0.5 }
We can use the $asin operator to return the arccosine of the data field:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
arcsine: { $asin: "$data" }
}
}
]
)
Result:
{ "arcsine" : 0.5235987755982988 }
By default, the $asin 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, $asin 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: { $asin: "$data" },
degrees: { $radiansToDegrees: { $asin: "$data" } }
}
}
]
)
Result:
{ "radians" : 0.5235987755982988, "degrees" : 29.999999999999996 }
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 $asin 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("0.1301023541559787031443874490659") }
Let’s run the the $asin operator against that document:
db.test.aggregate(
[
{ $match: { _id: 2 } },
{ $project: {
_id: 0,
arcsine: { $asin: "$data" }
}
}
]
)
Result:
{ "arcsine" : NumberDecimal("0.1304722105697547116336288178856500") }
The output is 128-bit decimal.
Null Values
Null values return null when using the $asin operator.
Suppose we add the following document to our collection:
{ "_id" : 3, "data" : null }
Let’s run the the $asin operator against that document:
db.test.aggregate(
[
{ $match: { _id: 3 } },
{ $project: {
_id: 0,
arcsine: { $asin: "$data" }
}
}
]
)
Result:
{ "arcsine" : null }
We can see that the result is null.
NaN Values
If the argument resolves to NaN, $asin returns NaN.
Example:
db.test.aggregate(
[
{ $match: { _id: 3 } },
{ $project: {
_id: 0,
arcsine: { $asin: 1 * "String" }
}
}
]
)
Result:
{ "arcsine" : NaN }
In this case the expression tried to multiple a number by a string, which resulted in NaN being returned.
Infinity
If the argument resolves to Infinity or -Infinity, the $asin operator returns an error.
Suppose we add the following document to our collection:
{ "_id" : 4, "data" : Infinity }
Let’s run $asin against the data field:
db.test.aggregate(
[
{ $match: { _id: 4 } },
{ $project: {
_id: 0,
arcsine: { $asin: "$data" }
}
}
]
)
Result:
uncaught exception: Error: command failed: {
"ok" : 0,
"errmsg" : "cannot apply $asin to inf, value must in [-1,1]",
"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
That’s the error I get in the mongo shell when running that code.
Non-Existent Fields
If the $asin operator is applied against a field that doesn’t exist, null is returned.
Example:
db.test.aggregate(
[
{ $match: { _id: 4 } },
{ $project: {
_id: 0,
arcsine: { $asin: "$wrong" }
}
}
]
)
Result:
{ "arcsine" : null }
Out of Range Values
Providing an out of range value to $asin will result in an error. We’ve already seen what happens when we provide Infinity to $asin. Now let’s use a more down to Earth value, like say, 2.
Suppose we add the following document to the collection:
{ "_id" : 5, "data" : 2 }
Now let’s run $asin against the data field:
db.test.aggregate(
[
{ $match: { _id: 5 } },
{ $project: {
_id: 0,
arcsine: { $asin: "$data" }
}
}
]
)
Result:
uncaught exception: Error: command failed: {
"ok" : 0,
"errmsg" : "cannot apply $asin to 2, value must in [-1,1]",
"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
As the error message indicates, the value must be between -1 and 1.