MongoDBでは、$switch 集計パイプラインオペレーターは、一連のcaseを評価します 式、およびcaseの場合にのみ指定された式を実行します 式はtrueと評価されます 。
構文
構文は次のようになります:
$switch: {
branches: [
{ case: <expression>, then: <expression> },
{ case: <expression>, then: <expression> },
...
],
default: <expression>
} 例
petsというコレクションがあるとします。 次のドキュメントを使用:
{ "_id" : 1, "name" : "Wag", "type" : "Dog", "weight" : 20 }
{ "_id" : 2, "name" : "Bark", "type" : "Dog", "weight" : 10 }
{ "_id" : 3, "name" : "Meow", "type" : "Cat", "weight" : 7 }
{ "_id" : 4, "name" : "Scratch", "type" : "Cat", "weight" : 8 }
{ "_id" : 5, "name" : "Bruce", "type" : "Kangaroo", "weight" : 100 }
{ "_id" : 6, "name" : "Hop", "type" : "Kangaroo", "weight" : 130 }
{ "_id" : 7, "name" : "Punch", "type" : "Kangaroo", "weight" : 200 }
{ "_id" : 8, "name" : "Snap", "type" : "Cat", "weight" : 12 }
{ "_id" : 9, "name" : "Ruff", "type" : "Dog", "weight" : 30 }
$switchを使用できます weightに対していくつかのcase式を実行する演算子 フィールド:
db.pets.aggregate(
[
{
$project:
{
_id: 0,
weight: 1,
result: {
$switch: {
branches: [
{ case: { $gt: [ "$weight", 100 ] }, then: "Heavy" },
{ case: { $lt: [ "$weight", 20 ] }, then: "Light" }
],
default: "Medium"
}
}
}
}
]
) 結果:
{ "weight" : 20, "result" : "Medium" }
{ "weight" : 10, "result" : "Light" }
{ "weight" : 7, "result" : "Light" }
{ "weight" : 8, "result" : "Light" }
{ "weight" : 100, "result" : "Medium" }
{ "weight" : 130, "result" : "Heavy" }
{ "weight" : 200, "result" : "Heavy" }
{ "weight" : 12, "result" : "Light" }
{ "weight" : 30, "result" : "Medium" } デフォルトの式を省略する
defaultを省略します コードからエラーが発生する可能性があります。ただし、これはcaseの結果によって異なります 式。
defaultを削除した場合 上記の例の一部では、エラーが発生します:
db.pets.aggregate(
[
{
$project:
{
_id: 0,
weight: 1,
result: {
$switch: {
branches: [
{ case: { $gt: [ "$weight", 100 ] }, then: "Heavy" },
{ case: { $lt: [ "$weight", 20 ] }, then: "Light" }
]
}
}
}
}
]
) 結果:
uncaught exception: Error: command failed: {
"ok" : 0,
"errmsg" : "$switch could not find a matching branch for an input, and no default was specified.",
"code" : 40066,
"codeName" : "Location40066"
} : 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:639:17
example@sqldat.com/mongo/shell/assert.js:729:16
example@sqldat.com/mongo/shell/db.js:266:5
example@sqldat.com/mongo/shell/collection.js:1058:12
@(shell):1:1
この場合、caseでカバーされていない入力値がありました 式(つまり、20〜100の式)、つまり$switch エラーを返しました。
ただし、caseを変更すると 式を少し変更すると、defaultを削除できます エラーのない部分:
db.pets.aggregate(
[
{
$project:
{
_id: 0,
weight: 1,
result: {
$switch: {
branches: [
{ case: { $gt: [ "$weight", 100 ] }, then: "Heavy" },
{ case: { $lte: [ "$weight", 100 ] }, then: "Light" }
]
}
}
}
}
]
) 結果:
{ "weight" : 20, "result" : "Light" }
{ "weight" : 10, "result" : "Light" }
{ "weight" : 7, "result" : "Light" }
{ "weight" : 8, "result" : "Light" }
{ "weight" : 100, "result" : "Light" }
{ "weight" : 130, "result" : "Heavy" }
{ "weight" : 200, "result" : "Heavy" }
{ "weight" : 12, "result" : "Light" }
{ "weight" : 30, "result" : "Light" }
この例では、すべてのドキュメントがすべてのcaseを満たしていました 式など、default 必要ありませんでした。つまり、エラーは発生しませんでした。
MongoDBドキュメント
詳細と例については、MongoDBのドキュメントを参照してください。