postgresql - Sequelize : Date Comparison Inside of an AND Block

标签 postgresql datetime sequelize.js

我为此苦苦思索了几个小时——试图在 and 块内进行日期比较。这两个部分让我困惑了一段时间——日期比较部分和 and 语法。

这是不起作用的语法 - 然后我会用有效的代码发布答案。

注意: user_presence_time_of_last_updatetime 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));

最佳答案

这是在 this SO post 的大力协助下确实有效的代码:

确实有效

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

关于postgresql - Sequelize : Date Comparison Inside of an AND Block,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62126817/

相关文章:

javascript - 如何在 Sequelize 中读取表的列属性?

PostgreSQL 将字符串的一部分更改为大写

django - 这个查询集循环是最优的吗?

php - 用于检查一个日期是否接近当前日期的 MySQL 查询

java - 将 Joda LocalDate 或 java.util.Date 转换为当天开始时的 LocalDateTime

java - 为什么我不应该在 Java TimeZone 中使用缩写

express - findOne 不是函数

sequelize.js - TypeError models.User.find 不是函数

postgresql - "order by foo, bar"和 "order by (foo, bar)"之间的差异(带括号)

postgresql - 从 Azure CLI(不是 ARC)创建 PostgreSQL 超大规模 Citus DB