In MongoDB, de $switch aggregatiepijplijnoperator evalueert een reeks case expressies, en voert een gespecificeerde expressie alleen uit wanneer een case uitdrukking evalueert tot true .
Syntaxis
De syntaxis gaat als volgt:
$switch: {
branches: [
{ case: <expression>, then: <expression> },
{ case: <expression>, then: <expression> },
...
],
default: <expression>
} Voorbeeld
Stel dat we een verzameling hebben met de naam pets met de volgende documenten:
{ "_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 }
We kunnen de $switch . gebruiken operator om enkele hoofdletterexpressies uit te voeren tegen het weight veld:
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"
}
}
}
}
]
) Resultaat:
{ "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" } De standaardexpressie weglaten
De default weglaten uit de code kan resulteren in een fout. Maar dit hangt af van de uitkomst van de case uitdrukkingen.
Als we de default . verwijderen deel van het bovenstaande voorbeeld krijgen we een foutmelding:
db.pets.aggregate(
[
{
$project:
{
_id: 0,
weight: 1,
result: {
$switch: {
branches: [
{ case: { $gt: [ "$weight", 100 ] }, then: "Heavy" },
{ case: { $lt: [ "$weight", 20 ] }, then: "Light" }
]
}
}
}
}
]
) Resultaat:
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
In dit geval waren er invoerwaarden die niet werden gedekt door de case uitdrukkingen (d.w.z. die tussen 20 en 100), en dus $switch heeft een fout geretourneerd.
Als we echter de case uitdrukkingen enigszins, kunnen we de default deel zonder fouten:
db.pets.aggregate(
[
{
$project:
{
_id: 0,
weight: 1,
result: {
$switch: {
branches: [
{ case: { $gt: [ "$weight", 100 ] }, then: "Heavy" },
{ case: { $lte: [ "$weight", 100 ] }, then: "Light" }
]
}
}
}
}
]
) Resultaat:
{ "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" }
In dit voorbeeld voldeden alle documenten aan alle case uitdrukkingen, en dus de default was niet nodig - wat betekende dat er geen fout werd geproduceerd.
MongoDB-documentatie
Zie de MongoDB-documentatie voor meer details en voorbeelden.