Sequelize-查询毫秒字段以获取相同日期的所有数据



在我的表test中,我存储具有毫秒值的字段date,例如1620287520000,它是UTC日期和时间中的Thu May 06 2021 07:52:00

假设我有一些低于的记录

[
{
id:1,
date:1620287520000
},  
{
id:2,
date:xxxx
},
]

我想获取dateThu May 06 2021的所有数据,而不考虑时间。

如何用Sequelize/nodejs的方式实现?

test.model.js

const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;
module.exports = function (app) {
const sequelizeClient = app.get('sequelizeClient');
const test = sequelizeClient.define('test', {
...,
date:{
type: DataTypes.DATE,
},
}, {
hooks: {
beforeCount(options) {
options.raw = true;
}
}
});
return test;
};

您可以在where选项中使用sequelize运算符,例如:

// first call Op from sequelize package
const {Op} = require('sequelize')
// calling data which is less than Thu May 07 2021 and Thu May 06 2021 with lt (less than) and gte (greater than equal) operators
const data = await Test.findAll({where: {
date: {
[Op.lt]: 'Thu May 07 2021', // not sure whether this would be valid date, if not change it to javascript valid date
[Op.gte]: 'Thu May 06 2021'
}
}})

最新更新