javascript Sequelize 如何查找具有多个 where 子句和时间戳的行 > NOW()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8390009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sequelize how to find rows with multiple where clauses and timestamp > NOW()
提问by lakenen
How do I do this with Sequelize?
我如何使用 Sequelize 做到这一点?
SELECT FROM sessions WHERE user_id = ? AND token = ? AND expires > NOW()
Here's what I'm trying to do (assume Session
is a Sequelize model):
这是我想要做的(假设Session
是一个 Sequelize 模型):
Session.find({
where: {
user_id: someNumber,
token: someString,
//expires > NOW() (how do I do this?)
}
}).on('success', function (s) { /* things and stuff */ });
Thanks!
谢谢!
回答by sdepold
did you try to use the array notation of finders in sequelize?
您是否尝试在续集中使用查找器的数组符号?
Session.find({
where: ['user_id=? and token=? and expires > NOW()', someNumber, someString]
}).on('success', function (s) { /* things and stuff */ });
You can checkout this page: http://sequelizejs.com/?active=find-objects#find-objects
您可以查看此页面:http: //sequelizejs.com/?active=find-objects#find- objects
Hope this works. Otherwise it's a bug :D
希望这有效。否则它是一个错误:D
回答by Peter Kingsbury
Another method:
另一种方法:
Session.find({
where: {
user_id: someNumber,
token: someString,
expires: {
$gt: (new Date())
}
}
}).on('success', function (s) { /* things and stuff */ });
回答by JR Smith
Please note that as of this posting and the most recent version of sequelize, the above answers are out of date. I am posting the solution that I got working in hopes of saving someone some time.
请注意,截至本帖和最新版本的 sequelize,上述答案已过时。我正在发布我正在工作的解决方案,希望能节省一些时间。
filters["State"] = {$and: [filters["State"], {$not: this.filterSBM()}] };
Note the $ and array inside the JSON object and lack of ? token replacement.
请注意 JSON 对象中的 $ 和数组以及缺少 ? 代币替换。
Or put in a more general way, since that might help someone wrap their head around it:
或者用更一般的方式,因为这可能会帮助人们理解它:
{ $and: [{"Key1": "Value1"}, {"Key2": "Value2"}] }