【问题标题】:Trying to query postgres JSON data type with sequelize尝试使用 sequelize 查询 postgres JSON 数据类型
【发布时间】:2016-10-07 13:57:36
【问题描述】:

我的表中有一个新创建的列,我需要在我的一个服务器控制器中进行查询。此列中的数据将是 JSON 类型并命名为“元”。完成后会是这个样子

{
  "audit": {"date": 1465311598315, "user": 20191932891}
}

目前,此表中的所有条目都有一个空 JSON 列,其值为 NULL。

在我的一个服务器控制器中,我需要获取 meta 为 null 或 meta.audit.date 超过 90 天的所有条目。我的查询看起来像这样

  db.Question.findAll({
  where: {
    status: 'active',
    PassageId: {
      $eq: null
    },
    meta: {
      audit: {
        date: {
          $or: {
            $lte: threeMonthsAgo,
            $eq: null
          }
        }
      }
    }
  }
});

在这种情况下,三个月前是三个月前的日期。

没有返回任何结果,我在控制台中收到此错误:

Unhandled rejection SequelizeDatabaseError: operator does not exist: text <= bigint

【问题讨论】:

    标签: node.js postgresql sequelize.js


    【解决方案1】:

    原来我没有正确使用 $or 运算符

    db.Question.findAll({
      where: {
        status: 'active',
        PassageId: {
          $eq: null
        },
        {
          $or: [{
            'meta.audit.date': {
              $eq: null
            }
          }, {
            'meta.audit.date': {
              $lte: threeMonthsAgo
            }
          }]
        }
    
      }
    });
    

    【讨论】:

    • 由于日期类型在 JSON 中存储为字符串,因此这里的 'meta.audit.date' 是一个字符串。在与我假设的日期 threeMonthsAgo 进行比较之前,您是否需要将其转换为日期?
    猜你喜欢
    • 2014-05-05
    • 2019-03-20
    • 2014-04-10
    • 2016-03-18
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2018-05-31
    • 2022-01-07
    相关资源
    最近更新 更多