在聚合时MongoDB中是否有elseif要进行$ cond的操作 [英] Is there an elseif thing in MongoDB to $cond while aggregating

查看:1921
本文介绍了在聚合时MongoDB中是否有elseif要进行$ cond的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我需要在MongoDB中按如下方式计算的自定义字段

So I need a custom field calculated in MongoDB as follows

if( field1 =="A")  ->customfield=10
else if(field1 =="B"  )->customfield=20
else (field1 =="C" ) ->customfield=15

我正在使用聚合以及$ project语句。但是$ cond运算符不允许elseif(对else进行子分支),而仅允许两个静态分支if and else。使用嵌套的elseif会导致

I'm using aggregation along with the $project statement. But the $cond operator doesn't allow elseif (subbranching the else) and merely allows two static branches if and else. Using a nested elseif causes

例外:$表达式内不允许包含字段

在这里查询(给我错误)

Heres my query(which gives me the error)

db.items.aggregate([ { $project :
{
     name: 1,
     customfield:
     {
         $cond: { if: { $eq: [ "$field1", "4" ] }, then: 30,
                else: {
                    if: 
                    { $eq: ["$field1","8"]}, 
                    then: 25, else: 10}}
               }
           }},{ $sort: { customfield: 1 }},{$limit:12}]);

是否有解决方法或解决方法。我很抱歉,如果这是一个重复的问题,但我找不到类似的问题。

Is there a method or workaround to this. My apologies if this is a repeated question but I wasn't able to find a similar one.

推荐答案

在现代版本中( MongoDB 3.4),您可以使用 $ switch ,基本上与其他语言实现中的 switch case 关键字相对应:

With modern releases ( since MongoDB 3.4 ) you would use $switch, which is basically the counterpart to switch or case keywords in other language implementations:

db.items.aggregate([
  { "$project": {
    "name": 1,
    "customfield": {
      "$switch": {
        "branches": [
          { "case": { "$eq": [ "$field1", "4" ] }, "then": 30 },
          { "case": { "$eq": [ "$field1", "8" ] }, "then": 25 }
        ],
        "default": 10
      }
    }
  }},
  { "$sort": { customfield: 1 }},
  { "$limit":12 }
])

这可以避免嵌套 if..then..else 条件使用 $ cond ,如下所示。但是下面的示例仍然显示可以始终执行此操作,即使在使用原始数组表示法的甚至显式 if..then..else 关键字的新运算符之前也可以执行此操作始终保持该语法。

This avoids nesting the if..then..else conditions as can be done using $cond and shown below. But the below still shows as an example that this could always be done, even before the new operator of even the explicit if..then..else keywords since the original array notation always maintained that syntax.

还要注意,此处的条件 array 通常比创建更容易以编程方式构造 <$ c $所需的语句的嵌套数据结构c> $ cond

Noting also that an array of conditions here is typically also a lot easier to construct programatically than creating a nested data structure for the statement as was needed with $cond.

if ..then..else 关键字添加到 <$在撰写本文时,c $ c> $ cond 运算符只是MongoDB的最新版本中的最新添加(MongoDB 2.6是 keywords 的引入在MongoDB 2中发布聚合框架后即可使用实际的运算符.2)。目的是为了清楚起见,但在这种情况下似乎引起了一些混乱。

The if..then..else keywords to the $cond operator are only a recent addition as of recent versions of MongoDB at the time of writing ( MongoDB 2.6 was the introduction of the keywords. The actual operator was available with release of the aggregation framework in MongoDB 2.2 ). The intention was for clarity but in this case it seems to has caused some confusion.

作为 if..then.else 运算符 $ cond 确实是 三元 运算符,就像用许多编程语言实现。这意味着作为不符合条件的内联条件,而不是对条件创建逻辑块,不符合第一个条件的任何事物都属于 else

因此,您嵌套了语句而不是遵循块:

Therefore you "nest" the statements rather than follow blocks:

db.items.aggregate([
  { "$project": {
    "name": 1,
    "customfield": {
      "$cond": { 
        "if": { "$eq": [ "$field1", "4" ] }, 
        "then": 30,
        "else": {
          "$cond": {
            "if": { "$eq": ["$field1","8"]}, 
            "then": 25, 
            "else": 10
          }
        }
      }
    }
  }},
  { "$sort": { customfield: 1 }},
  { "$limit":12 }
]);

甚至使用原始的 array 表示法,有些人可能会更喜欢该语句以编程方式:

Or even with the original array notation, which some might prefer if building the statement programatically:

db.items.aggregate([
  { "$project": {
    "name": 1,
    "customfield": {
      "$cond": [
         { "$eq": [ "$field1", "4" ] }, 
         30,
         { "$cond": [
           { "$eq": ["$field1","8"] },
           25, 
           10
         ]}
      ]
    }
  }},
  { "$sort": { customfield: 1 }},
  { "$limit":12 }
]);

三元表示三个条件,不多也不少。因此,所有 if..then..else 逻辑都必须嵌套。

Ternary means three conditions, no more no less. So all if..then..else logic must be nested.

这篇关于在聚合时MongoDB中是否有elseif要进行$ cond的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆