我的应用程序需要比较JSONB中的日期字段。该平台使用nodejs(10.16),postgres 11,moment (2.24.0)
和sequelize (5.7.6)
。这是sql代码:
let events = await Event.findAll({
where: {
status : "active",
fort_token: req.user.fort_token,
"event_info.finish_datetime": {[Op.ne]: null, [Op.gte]: moment().toString()},
access_list: { [Op.contains]: [req.user.id] }},
attributes: {exclude: ['fort_token']}
});
它将event_info.finish_datetime
中的日期与当前日期时间进行比较,并选择finish_datetime
在当前日期时间之后的记录。这是要查询的pq表:
在表格中,应该检索4条记录(id 49、50、51和52)(日期格式在行中并不完全相同)。
这是查询结果的控制台输出,仅检索到记录51:
Executing (default): SELECT "id", "name", "created_by_id", "event_info", "current_step", "access_list", "event_step", "event_snapshot", "event_category_id", "status", "last_updated_by_id", "createdAt", "updatedAt" FROM "events" AS "event" WHERE "event"."status" = 'active' AND "event"."fort_token" = 'SI3FBmtQDtyodVTXkTsYMp7PziKxwIwX' AND (("event"."event_info"#>>'{finish_datetime}') IS NOT NULL AND ("event"."event_info"#>>'{finish_datetime}') >= 'Wed Jul 17 2019 14:52:13 GMT-0700') AND "event"."access_list" @> ARRAY[22]::INTEGER[];
events : [ event {
dataValues:
{ id: 51,
name: 'test 3',
created_by_id: 22,
event_info: [Object],
current_step: null,
access_list: [Array],
event_step: null,
event_snapshot: null,
event_category_id: null,
status: 'active',
last_updated_by_id: 22,
createdAt: 2019-07-15T23:25:06.823Z,
updatedAt: 2019-07-15T23:25:06.823Z },
_previousDataValues:
检索所有4条记录的正确方法是什么?