MongoDBでは、$tan 集計パイプライン演算子は、ラジアンで測定された値のタンジェントを返します。
$tan 数値に解決される有効な式を受け入れます。
$tan 演算子はMongoDB4.2で導入されました。
例
testというコレクションがあるとします。 次のドキュメントで:
{ "_id" : 1, "data" : 2 }
$tanを使用できます dataの接線を返す演算子 フィールド:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
tangent: { $tan: "$data" }
}
}
]
) 結果:
{ "tangent" : -2.185039863261519 } ラジアンに変換
前述のように、$tan ラジアンで測定された値のタンジェントを返します。値が度単位の場合は、$degreesToRadiansを使用できます 演算子を使用してラジアンに変換します。
例:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
tangent: { $degreesToRadians: { $tan: "$data" } }
}
}
]
) 結果:
{ "tangent" : -0.038136139901240186 } 128ビットの10進値
デフォルトでは、$tan 演算子は値をdoubleとして返します 、ただし、式が128ビットの10進数値に解決される限り、128ビットの10進数として値を返すこともできます。
コレクションに次のドキュメントを追加するとします。
{ "_id" : 2, "data" : NumberDecimal("2.1301023541559787031443874490659") }
$tanを実行してみましょう そのドキュメントに対する演算子:
db.test.aggregate(
[
{ $match: { _id: 2 } },
{ $project: {
_id: 0,
tangent: { $tan: "$data" }
}
}
]
) 結果:
{ "tangent" : NumberDecimal("-1.597486946407534452195921861435753") } 出力は128ビットの10進数です。
ヌル値
ヌル値はnullを返します $tanを使用する場合 オペレーター。
コレクションに次のドキュメントを追加するとします。
{ "_id" : 3, "data" : null }
$tanを実行してみましょう そのドキュメントに対する演算子:
db.test.aggregate(
[
{ $match: { _id: 3 } },
{ $project: {
_id: 0,
result: { $tan: "$data" }
}
}
]
) 結果:
{ "result" : null }
結果がnullであることがわかります 。
NaN値
引数がNaNに解決された場合 、$tan NaNを返します 。
例:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
result: { $tan: 1 * "$data" }
}
}
]
) 結果:
{ "result" : NaN } 存在しないフィールド
$tanの場合 存在しないフィールド、nullに対して演算子が適用されます 返されます。
例:
db.test.aggregate(
[
{ $match: { _id: 1 } },
{ $project: {
_id: 0,
result: { $tan: "$name" }
}
}
]
) 結果:
{ "result" : null } インフィニティ
Infinityの提供 または-Infinity エラーを返します。
次のドキュメントをコレクションに追加するとします。
{ "_id" : 4, "data" : Infinity }
{ "_id" : 5, "data" : -Infinity }
$tanを適用しましょう これらのドキュメントへ:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 4, 5 ] } } },
{ $project: {
tangent: { $tan: "$data" }
}
}
]
) 結果:
uncaught exception: Error: command failed: {
"ok" : 0,
"errmsg" : "cannot apply $tan to inf, value must in (-inf,inf)",
"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
この場合、$tan エラーが発生する前の最初のドキュメント(ドキュメント4)までしか取得できませんでした。エラーメッセージが$tanを適用しようとしたことを示しているため、これがわかります。 to inf(Infinity 。
ドキュメント4をクエリフィルタから削除すると、ドキュメント5に進み、同じエラーが返されることがわかります。
db.test.aggregate(
[
{ $match: { _id: { $in: [ 5 ] } } },
{ $project: {
tangent: { $tan: "$data" }
}
}
]
) 結果:
uncaught exception: Error: command failed: {
"ok" : 0,
"errmsg" : "cannot apply $tan to -inf, value must in (-inf,inf)",
"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
今回のエラーは、$tanを適用しようとしたことを示しています to -inf(-Infinity 。