在两个where条件下运行sequelize



我有一个mysql-db实例,其中有一个由多个字段组成的表。相关字段为开始、开始时间和状态

  • 开始时间:YYYY-MM-DD
  • 开始时间:HH:mm:ss
  • 状态:ENUM("已取消"、"已安排"等(

如果我想获得状态不是"已取消"的所有条目的列表,并且这些条目发生在今天或之后,我会写下:

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,
}
}]
}
},
}
},

(不起作用,因为它只返回所有内容!(

我也不能做像这样简单的事情

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') }
]
}
...

最新更新