MongoDB聚合运算符:$switch

MongoDB聚合运算符:$switch


$switch聚合运算符评估一系列case表达式,当找到一个计算结果为 true的表达式时, $switch会执行指定的表达式并脱离控制流。

语法

$switch: {
   branches: [
      { case: <expression>, then: <expression> },
      { case: <expression>, then: <expression> },
      ...
   ],
   default: <expression>
}

branches数组中的对象必须仅包含case字段和then字段。

  • branches:控制分支文档的数组,数组至少要有一个元素,分支为包含以下字段的文档:
    • case:可解析为布尔值的表达式,如果不是布尔类型,则强制转换为布尔类型
    • then:可以是任何合法的表达式
  • default:可选字段,没有分支表达式为true时的路径。尽管是可选的,但如果未指定默认值并且没有分支为true时$switch将返回错误。

使用

各种case语句不需要相互排斥,$switch会执行第一个结果为true的分支,如果没有分支为true,$switch执行默认选项。

以下情况会导致$switch出错:

  • 分支字段缺失或不是至少有一个条目的数组。
  • 分支数组中的对象不包含case字段。
  • 分支数组中的对象不包含then字段。
  • 分支数组中的对象包含casethen以外的字段。
  • 未指定默认值,且case值均为true

第一个符合条件的分支

{
   $switch: {
      branches: [
         { case: { $eq: [ 0, 5 ] }, then: "equals" },
         { case: { $gt: [ 0, 5 ] }, then: "greater than" },
         { case: { $lt: [ 0, 5 ] }, then: "less than" }
      ]
   }
}

结果:

"less than"

缺省分支

{
   $switch: {
      branches: [
         { case: { $eq: [ 0, 5 ] }, then: "equals" },
         { case: { $gt: [ 0, 5 ] }, then: "greater than" }
      ],
      default: "Did not match"
   }
}

结果:

"Did not match"

强制转换case为布尔值

{
   $switch: {
      branches: [
         { case: "this is true", then: "first case" },
         { case: false, then: "second case" }
      ],
      default: "Did not match"
   }
}

结果:

"First case"

举例

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 ] }

下面的聚合操作使用$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."
            }
         }
      }
   }
] )

操作返回下面的结果:

{ "_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!" }
  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

原子星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值