MongoDB:使用 switch 函数创建计算字段(列别名)

MongoDB: Creating calculated fields using the switch function (column aliasing)

备注

我对 MongoDB 完全陌生,所以我的术语可能不完全正确。

上下文

我在 MongoDB 中有一些具有不同日期的数据,我正在尝试使用 $switch 函数来提供列别名以形成一些新的计算列 但是我运行遇到了一些问题(可能是语法或错误的实现),想知道为什么以及如何解决。

问题

基本上我想创建一个新的计算日期列,如果它不为空,它采用日期字段的值,否则它采用另一个日期字段的值,如果它不为空等等,但它默认为一些消息 "blank" 如果这些字段中的任何一个都没有值。

架构

包含集合的单个文档遵循此粗略结构

{
  _id:ObjectId("619756f12c115f24df503c26")
  uniqueid:"12345678"
  date1:"2021-11-02 20:04:50.253"
  date2:"2021-11-03 18:10:57.520"
  date3:"2021-11-08 07:08:00.000"
  date4:"2021-11-08 14:40:00.000"
  date5:"2021-11-08 08:34:00.000"
}

尝试过

作为聚合管道的一部分,我尝试使用 $project 然后返回所有列以及新的计算列但是在这个阶段出现错误说 stage must be a properly formatted document leading me相信我要么是我的语法错误,要么是使用了错误的运算符来执行此操作。

如果我删除计算列代码,那么预览似乎可以正常工作(进一步说服我它与我的计算列实现有关)。

db.collection.aggregate([
{
$project:
      {
      "uniqueid": 1,
      "date1": 1,
      "date2": 1,
      "date3": 1,
      "date4":1,
      "date5": 1,
      "cal_date1": {
          $switch: {
            branches: [
                { case: {"$date2": {$ne: null}}, then: "$date2"},
                { case: {"$date3": {$ne: null}}, then: "$date3"},
                { case: {"$date4": {$ne: null}}, then: "$date4"},
                { case: {"$date5": {$ne: null}}, then: "$date5"}
            ],
            default: "blank"
          }
        },
      "cal_date2": {
          $switch: {
            branches: [
                { case: {"$date4": {$ne: null}}, then: "$date4"},
                { case: {"$date5": {$ne: null}}, then: "$date5"}
            ],
            default: "blank"
          }
        },
       "cal_date3": {
          $switch: {
            branches: [
                { case: {"$date5": {$ne: null}}, then: "$date5"}
            ],
            default: "blank"
        }
      }
    }
}
])

更新 1:2021-11-24 T12:15pm UTC

我在每个 case 表达式的末尾添加了一个缺少的大括号。现在的错误是 unknown operator: $date2

描述

我的语句的语法似乎是导致问题的原因(不确定原因)。如果有人能详细说明就好了。

回答

代码如下:

db.collection.aggregate([
{
$project:
      {
      "uniqueid": 1,
      "date1": 1,
      "date2": 1,
      "date3": 1,
      "date4":1,
      "date5": 1,
      "cal_date1": {
          $switch: {
            branches: [
                { case: {$ne:["$date2",null]}, then: "$date2"},
                { case: {$ne:["$date3",null]}, then: "$date3"},
                { case: {$ne:["$date4",null]}, then: "$date4"},
                { case: {$ne:["$date5",null]}, then: "$date5"}
            ],
            default: "blank"
          }
        },
      "cal_date2": {
          $switch: {
            branches: [
                { case: {$ne:["$date4",null]}, then: "$date4"},
                { case: {$ne:["$date4",null]}, then: "$date5"}
            ],
            default: "blank"
          }
        },
       "cal_date3": {
          $switch: {
            branches: [
                { case: {$ne:["$date5",null]}, then: "$date5"}
            ],
            default: "blank"
        }
      }
    }
}
])

注意到每个语句中的 {"$date_value": {$ne: null}} 已更改为 {$ne:["$date_value",null]} 可能是由于 $ne 运算符需要 2 个参数?

将此 case: {"$date4": {$ne: null}} 更改为 case: {$ne:["$date4",null]},总计