【问题标题】:Mongoose - why cast date is failed?Mongoose - 为什么演员表日期失败?
【发布时间】:2021-03-24 13:54:05
【问题描述】:

我想从 2 个日期范围内的集合中过滤值:

const filterPattern = {
  createdAt: {
    '&gte': 2020-10-09T14:18:07.240Z,
    '&lte': 2020-12-09T14:26:49.544Z
  }
};

CollectionName.find(filterPattern);

但是得到一个错误:

CastError: Cast to date failed for value "{ '&gte': 2020-10-09T, '&lte': 2020-12-09T14:26:49.544Z }" at path "createdAt" for...

'&gte''&lte' 这两个值都是我由 new Date() 创建的,它们返回 trueinstanceof Date 之后。

那里有什么问题?

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    您必须使用$ 而不是& 并相应地设置值:

    const filterPattern = {
      createdAt: {
         $gte: "2020-10-09T14:18:07.240Z",
         $lte: "2020-12-09T14:26:49.544Z"
      }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2022-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多