Di MongoDB, $switch
operator pipa agregasi mengevaluasi serangkaian case
ekspresi, dan mengeksekusi ekspresi tertentu hanya ketika case
ekspresi dievaluasi menjadi true
.
Sintaks
Sintaksnya seperti ini:
$switch: {
branches: [
{ case: <expression>, then: <expression> },
{ case: <expression>, then: <expression> },
...
],
default: <expression>
}
Contoh
Misalkan kita memiliki koleksi yang disebut pets
dengan dokumen sebagai berikut:
{ "_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 }
Kita dapat menggunakan $switch
operator untuk menjalankan beberapa ekspresi kasus terhadap weight
bidang:
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"
}
}
}
}
]
)
Hasil:
{ "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" }
Menghilangkan Ekspresi Default
Menghilangkan default
dari kode dapat mengakibatkan kesalahan. Tapi ini tergantung pada hasil case
ekspresi.
Jika kami menghapus default
bagian dari contoh di atas, kami mendapatkan kesalahan:
db.pets.aggregate(
[
{
$project:
{
_id: 0,
weight: 1,
result: {
$switch: {
branches: [
{ case: { $gt: [ "$weight", 100 ] }, then: "Heavy" },
{ case: { $lt: [ "$weight", 20 ] }, then: "Light" }
]
}
}
}
}
]
)
Hasil:
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
Dalam hal ini, ada nilai input yang tidak tercakup oleh case
ekspresi (yaitu antara 20 dan 100), dan dengan demikian $switch
mengembalikan kesalahan.
Namun, jika kita mengubah case
ekspresi sedikit, kita dapat menghapus default
bagian tanpa kesalahan:
db.pets.aggregate(
[
{
$project:
{
_id: 0,
weight: 1,
result: {
$switch: {
branches: [
{ case: { $gt: [ "$weight", 100 ] }, then: "Heavy" },
{ case: { $lte: [ "$weight", 100 ] }, then: "Light" }
]
}
}
}
}
]
)
Hasil:
{ "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" }
Dalam contoh ini, semua dokumen memenuhi semua case
ekspresi, dan sebagai default
tidak diperlukan – yang berarti tidak ada kesalahan yang dihasilkan.
Dokumentasi MongoDB
Lihat dokumentasi MongoDB untuk detail dan contoh lebih lanjut.