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 के विरुद्ध कुछ केस एक्सप्रेशन चलाने के लिए फ़ील्ड:
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 दस्तावेज़ देखें।