$switch (aggregation)
On this page本页内容
Definition定义
$switch
-
Evaluates a series of case expressions. When it finds an expression which evaluates to计算一系列大小写表达式。当它找到一个计算结果为true
,$switch
executes a specified expression and breaks out of the control flow.true
的表达式时,$switch
执行指定的表达式并脱离控制流。$switch
has the following syntax:具有以下语法:$switch: {
branches: [
{ case: <expression>, then: <expression> },
{ case: <expression>, then: <expression> },
...
],
default: <expression>
}The objects in thebranches
array must contain only acase
field and athen
field.branches
数组中的对象必须只包含一个case
字段和一个then
字段。Operand运算数Description描述branches
An array of control branch documents. Each branch is a document with the following fields:一组控制分支文档。每个分支都是一个包含以下字段的文档:-
case
Can be any valid expression that resolves to a可以是解析为boolean
.boolean
的任何有效表达式。If the result is not a如果结果不是boolean
, 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
的更多信息可以在这里找到。
-
then
Can be any valid expression.可以是任何有效的表达式。
Thebranches
array must contain at least one branch document.branches
数组必须至少包含一个分支文档。default
Optional.可选的。The path to take if no branch如果没有分支case
expression evaluates totrue
.case
表达式的计算结果为true
,则要采用的路径。
Although optional, if虽然是可选的,但如果未指定default
is unspecified and no branchcase
evaluates to true,$switch
returns an error.default
值,并且没有分支大小写计算为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
. true
。If none of the branches evaluates to true, 如果没有一个分支的计算结果为$switch
executes the default
option.true
,$switch
将执行默认选项。
The following conditions cause 以下情况导致$switch
to fail with an error:$switch
失败并出现错误:
Thebranches
field is missing or is not an array with at least one entry.branches
字段丢失或不是至少包含一个条目的数组。An object in thebranches
array does not contain acase
field.branches
数组中的对象不包含case
字段。An object in thebranches
array does not contain athen
field.branches
数组中的对象不包含then
字段。An object in thebranches
array contains a field other thancase
orthen
.branches
数组中的对象包含除case
或then
之外的字段。No没有指定default
is specified and nocase
evaluates 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!" }