Sequelize Where statement with date(Sequelize 带有日期的 Where 语句)
问题描述
我使用 Sequelize 作为我的后端 ORM.现在我想在日期上做一些 WHERE 操作.
I am using Sequelize as my backend ORM. Now I wish to do some WHERE operations on a Date.
更具体地说,我想获取日期介于现在和 7 天前之间的所有数据.
More specifically, I want to get all data where a date is between now and 7 days ago.
问题是文档没有指定可以对 Datatypes.DATE
The problem is that the documentation does not specify which operations you can do on Datatypes.DATE
谁能指出我正确的方向?
Can anyone point me in the right direction?
推荐答案
就像 Molda 说的,你可以使用 $gt, $lt, $gte 或 $lte 带日期:
Just like Molda says, you can use $gt, $lt, $gte or $lte with a date:
model.findAll({
where: {
start_datetime: {
$gte: moment().subtract(7, 'days').toDate()
}
}
})
如果您使用的是 Sequelize v5,则必须包含 Op 因为密钥已移至 Symbol
If you're using v5 of Sequelize, you've to include Op because the key was moved into Symbol
const { Op } = require('sequelize')
model.findAll({
where: {
start_datetime: {
[Op.gte]: moment().subtract(7, 'days').toDate()
}
}
})
在此处查看更多续集文档
这篇关于Sequelize 带有日期的 Where 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Sequelize 带有日期的 Where 语句
基础教程推荐
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
