Definition定义
$switchEvaluates a series of case expressions. When it finds an expression which evaluates to计算一系列大小写表达式。当它找到一个计算结果为true,$switchexecutes a specified expression and breaks out of the control flow.true的表达式时,$switch会执行指定的表达式并脱离控制流。$switchhas the following syntax:具有以下语法:$switch: {
branches: [
{ case: <expression>, then: <expression> },
{ case: <expression>, then: <expression> },
...
],
default: <expression>
}The objects in thebranchesarray must contain only acasefield and athenfield.branches数组中的对象必须仅包含case字段和then字段。Operand操作数Description描述branchesAn array of control branch documents. Each branch is a document with the following fields:一系列控制分支文档。每个分支都是一个包含以下字段的文档:-
caseCan be any valid expression that resolves to a可以是解析为boolean. If the result is not aboolean, it is coerced to a boolean value. More information about how MongoDB evaluates expressions as either true or false can be found here.boolean的任何有效表达式。如果结果不是布尔值,则将其强制转换为boolean。有关MongoDB如何将表达式评估为true或false的更多信息,请参阅此处。
-
thenCan be any valid expression.可以是任何有效的表达式。
Thebranchesarray must contain at least one branch document.branches数组必须至少包含一个分支文档。defaultOptional.可选。The path to take if no branch如果没有分支caseexpression evaluates totrue.case表达式的计算结果为true,则应采取的路径。Although optional, if虽然是可选的,但如果未指定defaultis unspecified and no branchcaseevaluates to true,$switchreturns an error.default并且没有分支case计算为true,$switch将返回错误。-
Behavior行为
The various case statements do not need to be mutually exclusive. 各种$switch executes the first branch it finds which evaluates to true. If none of the branches evaluates to true, $switch executes the default option.case语句不需要相互排斥。$switch执行它找到的第一个计算结果为true的分支。如果没有一个分支的计算结果为true,$switch将执行默认选项。
The following conditions cause 以下情况会导致$switch to fail with an error:$switch失败并出现错误:
Thebranchesfield is missing or is not an array with at least one entry.branches字段缺失或不是至少有一个条目的数组。An object in thebranchesarray does not contain acasefield.branches数组中的对象不包含case字段。An object in thebranchesarray does not contain athenfield.branches数组中的对象不包含then字段。An object in thebranchesarray contains a field other thancaseorthen.branches数组中的对象包含case或then以外的字段。No没有defaultis specified and nocaseevaluates totrue.default默认值,也没有case计算结果为true。
| "less than" |
| "Did not match" |
| "First case" |
Example示例
A collection named 名为grades contains the following documents:grades的集合包含以下文档:
{ "_id" : 1, "name" : "Susan Wilkes", "scores" : [ 87, 86, 78 ] }
{ "_id" : 2, "name" : "Bob Hanna", "scores" : [ 71, 64, 81 ] }
{ "_id" : 3, "name" : "James Torrelio", "scores" : [ 91, 84, 97 ] }
The following aggregation operation uses 以下聚合操作使用$switch to display a particular message based on each student's average score.$switch根据每个学生的平均成绩显示特定消息。
db.grades.aggregate( [
{
$project:
{
"name" : 1,
"summary" :
{
$switch:
{
branches: [
{
case: { $gte : [ { $avg : "$scores" }, 90 ] },
then: "Doing great!"
},
{
case: { $and : [ { $gte : [ { $avg : "$scores" }, 80 ] },
{ $lt : [ { $avg : "$scores" }, 90 ] } ] },
then: "Doing pretty well."
},
{
case: { $lt : [ { $avg : "$scores" }, 80 ] },
then: "Needs improvement."
}
],
default: "No scores found."
}
}
}
}
] )
The operation returns the following:该操作返回以下内容:
{ "_id" : 1, "name" : "Susan Wilkes", "summary" : "Doing pretty well." }
{ "_id" : 2, "name" : "Bob Hanna", "summary" : "Needs improvement." }
{ "_id" : 3, "name" : "James Torrelio", "summary" : "Doing great!" }