In MongoDB, il $switch l'operatore della pipeline di aggregazione valuta una serie di case espressioni ed esegue un'espressione specificata solo quando un case espressione restituisce true .
Sintassi
La sintassi è questa:
$switch: {
branches: [
{ case: <expression>, then: <expression> },
{ case: <expression>, then: <expression> },
...
],
default: <expression>
} Esempio
Supponiamo di avere una collezione chiamata pets con i seguenti documenti:
{ "_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 }
Possiamo usare il $switch operatore per eseguire alcune espressioni case rispetto al weight campo:
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"
}
}
}
}
]
) Risultato:
{ "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" } Omissione dell'espressione predefinita
Omissione del default dal codice può causare un errore. Ma questo dipende dall'esito del case espressioni.
Se rimuoviamo il default a parte l'esempio sopra, otteniamo un errore:
db.pets.aggregate(
[
{
$project:
{
_id: 0,
weight: 1,
result: {
$switch: {
branches: [
{ case: { $gt: [ "$weight", 100 ] }, then: "Heavy" },
{ case: { $lt: [ "$weight", 20 ] }, then: "Light" }
]
}
}
}
}
]
) Risultato:
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 questo caso, c'erano valori di input che non erano coperti dal case espressioni (cioè quelle comprese tra 20 e 100), e così $switch ha restituito un errore.
Tuttavia, se cambiamo il case espressioni leggermente, possiamo rimuovere il default parte senza errori:
db.pets.aggregate(
[
{
$project:
{
_id: 0,
weight: 1,
result: {
$switch: {
branches: [
{ case: { $gt: [ "$weight", 100 ] }, then: "Heavy" },
{ case: { $lte: [ "$weight", 100 ] }, then: "Light" }
]
}
}
}
}
]
) Risultato:
{ "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 questo esempio, tutti i documenti hanno soddisfatto tutti i case espressioni, e quindi il default non era necessario, il che significava che non veniva prodotto alcun errore.
Documentazione MongoDB
Consulta la documentazione di MongoDB per maggiori dettagli ed esempi.