0

目前无法弄清楚为什么一个管道有效而另一个管道无效。我从 MongoDB 图表中获得了两个管道,它们都返回了一些东西并在 MongoDBCharts 上显示图表。但是,当我在代码中使用它们时,只有第一个管道会返回一些东西。我对所有情况都使用了相同的数据。任何建议将不胜感激!

第一个不过滤过去 30 天(由 Mongo 硬编码),两个管道都是从 Mongodb 图表复制的,并且没有更改。

[
  {
    "$addFields": {
      "trigger_time": {
        "$convert": {
          "input": "$trigger_time",
          "to": "date",
          "onError": null
        }
      }
    }
  },
  {
    "$match": {
      "event_type": {
        "$nin": [
          null,
          "",
          "AC Lost",
          "Device Lost",
          "logged into Database",
          "logged into Nexus Database",
          "logged out of Nexus Database",
          "Low Battery"
        ]
      }
    }
  },
  {
    "$addFields": {
      "trigger_time": {
        "$cond": {
          "if": {
            "$eq": [
              {
                "$type": "$trigger_time"
              },
              "date"
            ]
          },
          "then": "$trigger_time",
          "else": null
        }
      }
    }
  },
  {
    "$addFields": {
      "__alias_0": {
        "hours": {
          "$hour": "$trigger_time"
        }
      }
    }
  },
  {
    "$group": {
      "_id": {
        "__alias_0": "$__alias_0"
      },
      "__alias_1": {
        "$sum": 1
      }
    }
  },
  {
    "$project": {
      "_id": 0,
      "__alias_0": "$_id.__alias_0",
      "__alias_1": 1
    }
  },
  {
    "$project": {
      "y": "$__alias_1",
      "x": "$__alias_0",
      "_id": 0
    }
  },
  {
    "$sort": {
      "x.hours": 1
    }
  },
  {
    "$limit": 5000
  }
]

第二个

[
  {
    "$addFields": {
      "trigger_time": {
        "$convert": {
          "input": "$trigger_time",
          "to": "date",
          "onError": null
        }
      }
    }
  },
  {
    "$match": {
      "event_type": {
        "$nin": [
          null,
          "",
          "AC Lost",
          "Device Lost",
          "logged into Database",
          "logged into Nexus Database",
          "logged out of Nexus Database",
          "Low Battery"
        ]
      },
      "trigger_time": {
        "$gte": {
          "$date": "2021-03-29T08:35:47.804Z"
        }
      }
    }
  },
  {
    "$addFields": {
      "trigger_time": {
        "$cond": {
          "if": {
            "$eq": [
              {
                "$type": "$trigger_time"
              },
              "date"
            ]
          },
          "then": "$trigger_time",
          "else": null
        }
      }
    }
  },
  {
    "$addFields": {
      "__alias_0": {
        "hours": {
          "$hour": "$trigger_time"
        }
      }
    }
  },
  {
    "$group": {
      "_id": {
        "__alias_0": "$__alias_0"
      },
      "__alias_1": {
        "$sum": 1
      }
    }
  },
  {
    "$project": {
      "_id": 0,
      "__alias_0": "$_id.__alias_0",
      "__alias_1": 1
    }
  },
  {
    "$project": {
      "y": "$__alias_1",
      "x": "$__alias_0",
      "_id": 0
    }
  },
  {
    "$sort": {
      "x.hours": 1
    }
  },
  {
    "$limit": 5000
  }
]

4

1 回答 1

0

我最终解决了我自己的问题。经过一番挖掘和询问。Node.js 在使用 '$date' 时对 Mongodb 做了一些有趣的事情,这就是管道不起作用的原因。

解决方法是删除 '$date' 并传入一个日期对象。就我而言,

"trigger_time": {
        "$gte": new Date("2021-03-29T08:35:47.804Z")
      }
于 2021-05-07T01:59:12.347 回答