运行 使用两个 where 条件进行续集

Running sequelize with two where conditions

我有一个 mysql 数据库实例,其中 table 由多个字段组成。相关字段是开始、开始时间和状态

如果我想获得没有状态 = 'cancelled' 并且发生在今天或之后的所有条目的列表,我会这样写:

  return {
  where: {
    status: {
      $ne: 'cancelled'
    },
    $or: {
      start: { $gte: moment().utc().format('YYYY-MM-DD') },
      $and: {
        isRepeating: 1,
        $or: [{
          end: {
            $gte: moment().format(),
          }
        },
        {
          end: {
            $eq: null,
          }
        }]
      },
    }
  },

我正在尝试修改此查询,以便不仅提供今天或之后发生的条目,而且还提供大于现在的条目(时间明智,UTC)。我的尝试是先根据startTime过滤,再根据startDate过滤,但是好像不行:

    return {
  where: {
    status: {
      $ne: 'cancelled'
    },
    $or: {
      startTime: { $gt: moment.utc().format('HH:mm:ss') },
      $and: {
      
          start: { $gte: moment().utc().format('YYYY-MM-DD') },
        
        $and: {
        isRepeating: 1,
        $or: [{
          end: {
            $gte: moment().format(),
          }
        },
        {
          end: {
            $eq: null,
          }
        }]
      }
      },
    }
  },

(不起作用,因为它只是 returns 一切!)

我也不能做更简单的事情,比如

where: {
startTime: { $gt: moment.utc().format('HH:mm:ss') },
start: { $gte: moment().utc().format('YYYY-MM-DD') },
}

因为它会忽略,例如,明天发生的条目,但发生在当天早于当前时间戳。

谢谢!

您可以使用 Op.and 运算符来组合这些条件。

const { Op } = require("sequelize");

...
where: {
  [Op.and]: [
    startTime: { $gt: moment.utc().format('HH:mm:ss') },
    start: { $gte: moment().utc().format('YYYY-MM-DD') }
  ]
}
...