我为此敲定了几个小时 - 试图在and
块内进行日期比较。这两个部分都让我困惑了一段时间——日期比较部分和and
语法。
这是不起作用的语法 - 然后我将使用确实有效的代码发布答案。
注意:user_presence_time_of_last_update
是类型为time stamp with timezone
的字段。
不起作用
const op = connectors.Sequelize.Op;
const timeTwoHoursAgo = new Date()
timeTwoHoursAgo.setMinutes(timeTwoHoursAgo.getMinutes() - 120)
var usersWhoWentOffline = connectors.epUserData.findAll({
where: {
$and:{
user_presence_time_of_last_update: {$lt: new Date(new Date() - 5 * one_hour)},
$or:[
{user_presence: USER_PRESENCE_IDLE},
{user_presence: USER_PRESENCE_ONLINE},
],
}
}
}).then((res) => res.map((item) => item.dataValues));
这是确实有效的代码,这篇 SO 帖子提供了很大的帮助:
确实有效
const op = connectors.Sequelize.Op;
const timeTwoHoursAgo = new Date()
timeTwoHoursAgo.setMinutes(timeTwoHoursAgo.getMinutes() - 120)
var usersWhoWentOffline = connectors.epUserData.findAll({
where: {
[op.and]: {
user_presence_time_of_last_update: {
[op.lt]: timeTwoHoursAgo
},
[op.or]: [
{user_presence: USER_PRESENCE_IDLE},
{user_presence: USER_PRESENCE_ONLINE},
]
}
}
})