I MongoDB, $switch
aggregeringspipeline-operatören utvärderar en serie case
uttryck, och exekverar ett specificerat uttryck endast när ett case
uttryck utvärderas till true
.
Syntax
Syntaxen ser ut så här:
$switch: {
branches: [
{ case: <expression>, then: <expression> },
{ case: <expression>, then: <expression> },
...
],
default: <expression>
}
Exempel
Anta att vi har en samling som heter pets
med följande dokument:
{ "_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 }
Vi kan använda $switch
operatorn för att köra några kasusuttryck mot weight
fält:
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"
}
}
}
}
]
)
Resultat:
{ "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" }
Utesluter standarduttrycket
Utelämnar default
från koden kan resultera i ett fel. Men detta beror på resultatet av case
uttryck.
Om vi tar bort default
del av exemplet ovan får vi ett felmeddelande:
db.pets.aggregate(
[
{
$project:
{
_id: 0,
weight: 1,
result: {
$switch: {
branches: [
{ case: { $gt: [ "$weight", 100 ] }, then: "Heavy" },
{ case: { $lt: [ "$weight", 20 ] }, then: "Light" }
]
}
}
}
}
]
)
Resultat:
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 : [email protected]/mongo/shell/utils.js:25:13 [email protected]/mongo/shell/assert.js:18:14 [email protected]/mongo/shell/assert.js:639:17 [email protected]/mongo/shell/assert.js:729:16 [email protected]/mongo/shell/db.js:266:5 [email protected]/mongo/shell/collection.js:1058:12 @(shell):1:1
I det här fallet fanns det indatavärden som inte täcktes av case
uttryck (dvs. de mellan 20 och 100), och så $switch
returnerade ett fel.
Men om vi ändrar case
uttryck något, kan vi ta bort default
del utan fel:
db.pets.aggregate(
[
{
$project:
{
_id: 0,
weight: 1,
result: {
$switch: {
branches: [
{ case: { $gt: [ "$weight", 100 ] }, then: "Heavy" },
{ case: { $lte: [ "$weight", 100 ] }, then: "Light" }
]
}
}
}
}
]
)
Resultat:
{ "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" }
I det här exemplet uppfyllde alla dokument alla case
uttryck, och så default
behövdes inte – vilket innebar att inget fel uppstod.
MongoDB-dokumentation
Se MongoDB-dokumentationen för mer detaljer och exempel.