所以我需要一个在MongoDB中计算的自定义字段,如下所示

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

我在使用聚合和$project语句.但是$cond操作符不允许elseif(对else进行分支),只允许两个静态分支if和else.使用嵌套的elseif

"exception: field inclusion is not allowed inside of $expressions"

这是我的问题(这给了我错误)

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

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

推荐答案

对于现代版本(自MongoDB 3.4以来),您将使用$switch,这基本上相当于其他语言实现中的switchcase个关键字:

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关键字的新运算符出现之前,也可以始终这样做,因为原始数组表示法始终保持该语法.

还需要注意的是,这里的array个条件通常也比为语句创建nested个数据 struct 容易得多,这是$cond所需要的.


$cond运算符中的if..then..else个关键字只是在 compose 本文时MongoDB最新版本的基础上增加的(MongoDB 2.6是keywords的引入.实际的运算符可以在MongoDB 2.2中发布聚合框架).目的是为了澄清,但在本案中,它似乎引起了一些混乱.

作为if..then.else运算符,$cond实际上是ternary运算符,就像在许多编程语言中实现的那样.这意味着,作为一个"内联"条件,任何不满足第一个条件的东西都属于else,而不是为条件创建逻辑"块".

因此,您可以"嵌套"语句,而不是遵循块:

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符号,如果以编程方式构建语句,有些人可能会更喜欢:

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个逻辑都必须嵌套.

Mongodb相关问答推荐

MongoDB通过查找具有多个数组的对象进行聚合

从MongoDB中的嵌套数组中提取找到的值及其索引

如何用Python和MongoDB找到单据字段的最大值?

联接不返回具有ObjectId和非ObjectId的结果

执行聚合以消除重复并获得唯一计数

如何使用内部数组中的值更新文档

使用Go的Mongo驱动程序,从MongoDB文档中获取元素并更新其值需要帮助

Mongodb Timeseries / Golang - ['timestamp' 必须存在并包含有效的 BSON UTC 日期时间值]

如何在查找 foreignField 中使用通配符?

在 Mongo 聚合中,可以通过分组生成 3 个不同的计数

如何在 MongoDB 中已经存在的 slug 中添加一个随机数?

找到一个用户,然后使用 MongoDB 根据他们的总分获得他们的排名

有谁知道这个错误的修复方法(TypeError: Cannot assign to read only property ‘map’ of object '#')

使用 homebrew 和 Xcode 8.1.1 安装 Mongodb 失败

我怎样才能排序空值在 mongodb 中是最后排序的?

如何解决 ClassNotFoundException:com.mongodb.connection.BufferProvider?

MongoDB:自动生成的 ID 为零

mongoose字符串到 ObjectID

停止副本集 MongoDB

何时使用Singleton单例、Transient和使用 Ninject 和 MongoDB 的请求