MongoDBでは、$asin 集計パイプライン演算子は、ラジアンで測定された値のアークサイン(逆サイン)を返します。
$asin -1の間の数値に解決される有効な式を受け入れます および1 。
$asin 演算子はMongoDB4.2で導入されました。
例
testというコレクションがあるとします。 次のドキュメントで:
{ "_id" : 1, "data" : 0.5 }
$asinを使用できます dataのアークコサインを返す演算子 フィールド:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
arcsine: { $asin: "$data" }
}
}
]
) 結果:
{ "arcsine" : 0.5235987755982988 }
デフォルトでは、$asin 演算子は値をdoubleとして返します 、ただし、式が128ビットの10進数値に解決される限り、128ビットの10進数として値を返すこともできます。
度に変換
前述のように、$asin 結果をラジアンで返します。 $radiansToDegreesを使用できます 結果を度で表示したい場合は演算子。
例:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
radians: { $asin: "$data" },
degrees: { $radiansToDegrees: { $asin: "$data" } }
}
}
]
) 結果:
{ "radians" : 0.5235987755982988, "degrees" : 29.999999999999996 } この例では、最初のフィールドは結果をラジアンで表示し、2番目のフィールドは結果を度で表示します。
128ビットの10進値
式が$asinに提供された場合 が128ビットの10進数の場合、結果は128ビットの10進数で返されます。
コレクションに次のドキュメントを追加するとします。
{ "_id" : 2, "data" : NumberDecimal("0.1301023541559787031443874490659") }
$asinを実行してみましょう そのドキュメントに対する演算子:
db.test.aggregate(
[
{ $match: { _id: 2 } },
{ $project: {
_id: 0,
arcsine: { $asin: "$data" }
}
}
]
) 結果:
{ "arcsine" : NumberDecimal("0.1304722105697547116336288178856500") } 出力は128ビットの10進数です。
ヌル値
ヌル値はnullを返します $asinを使用する場合 オペレーター。
コレクションに次のドキュメントを追加するとします。
{ "_id" : 3, "data" : null }
$asinを実行してみましょう そのドキュメントに対する演算子:
db.test.aggregate(
[
{ $match: { _id: 3 } },
{ $project: {
_id: 0,
arcsine: { $asin: "$data" }
}
}
]
) 結果:
{ "arcsine" : null }
結果がnullであることがわかります 。
NaN値
引数がNaNに解決された場合 、$asin NaNを返します 。
例:
db.test.aggregate(
[
{ $match: { _id: 3 } },
{ $project: {
_id: 0,
arcsine: { $asin: 1 * "String" }
}
}
]
) 結果:
{ "arcsine" : NaN }
この場合、式は文字列で数値を乗算しようとしたため、NaNになりました。 返送されます。
インフィニティ
引数がInfinityに解決された場合 または-Infinity 、$asin オペレーターがエラーを返します。
コレクションに次のドキュメントを追加するとします。
{ "_id" : 4, "data" : Infinity }
$asinを実行してみましょう データフィールドに対して:
db.test.aggregate(
[
{ $match: { _id: 4 } },
{ $project: {
_id: 0,
arcsine: { $asin: "$data" }
}
}
]
) 結果:
uncaught exception: Error: command failed: {
"ok" : 0,
"errmsg" : "cannot apply $asin to inf, value must in [-1,1]",
"code" : 50989,
"codeName" : "Location50989"
} : aggregate failed :
example@sqldat.com/mongo/shell/utils.js:25:13
example@sqldat.com/mongo/shell/assert.js:18:14
example@sqldat.com/mongo/shell/assert.js:618:17
example@sqldat.com/mongo/shell/assert.js:708:16
example@sqldat.com/mongo/shell/db.js:266:5
example@sqldat.com/mongo/shell/collection.js:1046:12
@(shell):1:1
これがmongoで発生するエラーです そのコードを実行するときのシェル。
存在しないフィールド
$asinの場合 存在しないフィールド、nullに対して演算子が適用されます 返されます。
例:
db.test.aggregate(
[
{ $match: { _id: 4 } },
{ $project: {
_id: 0,
arcsine: { $asin: "$wrong" }
}
}
]
) 結果:
{ "arcsine" : null } 範囲外の値
$asinに範囲外の値を提供する エラーが発生します。 Infinityを提供するとどうなるかはすでに見てきました。 $asinへ 。それでは、たとえば2のように、地球の価値をもっと下げてみましょう。 。
次のドキュメントをコレクションに追加するとします。
{ "_id" : 5, "data" : 2 }
それでは、$asinを実行してみましょう。 dataに対して フィールド:
db.test.aggregate(
[
{ $match: { _id: 5 } },
{ $project: {
_id: 0,
arcsine: { $asin: "$data" }
}
}
]
) 結果:
uncaught exception: Error: command failed: {
"ok" : 0,
"errmsg" : "cannot apply $asin to 2, value must in [-1,1]",
"code" : 50989,
"codeName" : "Location50989"
} : aggregate failed :
example@sqldat.com/mongo/shell/utils.js:25:13
example@sqldat.com/mongo/shell/assert.js:18:14
example@sqldat.com/mongo/shell/assert.js:618:17
example@sqldat.com/mongo/shell/assert.js:708:16
example@sqldat.com/mongo/shell/db.js:266:5
example@sqldat.com/mongo/shell/collection.js:1046:12
@(shell):1:1
エラーメッセージが示すように、値は-1の間にある必要があります および1 。