Sequelize how to find rows with multiple where clauses and timestamp > NOW()

随声附和 提交于 2020-06-10 07:37:28

问题


How do I do this with 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.find({
    where: {
        user_id: someNumber,
        token: someString,
        //expires > NOW() (how do I do this?)
    }
}).on('success', function (s) { /* things and stuff */ });

Thanks!


回答1:


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

Hope this works. Otherwise it's a bug :D




回答2:


Another method:

Session.find({
  where: {
    user_id: someNumber,
    token: someString,
    expires: {
      $gt: (new Date())
    }
  }
}).on('success', function (s) { /* things and stuff */ });



回答3:


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.

filters["State"] = {$and: [filters["State"], {$not: this.filterSBM()}] };

Note the $ and array inside the JSON object and lack of ? token replacement.

Or put in a more general way, since that might help someone wrap their head around it:

{ $and: [{"Key1": "Value1"}, {"Key2": "Value2"}] }


来源:https://stackoverflow.com/questions/8390009/sequelize-how-to-find-rows-with-multiple-where-clauses-and-timestamp-now

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!