Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do query based on "date" column in sqlite3 using sequelize?

I'm using Sequelize with sqlite3 but having trouble querying data based on "date" type columns.

Here is my model definition:

var sensorData = sequelize.define('Data', 
{
    time: Sequelize.DATE,
    value: Sequelize.FLOAT,
    type: Sequelize.STRING,
    source: Sequelize.STRING
},
{
    timestamps : false
});

Now I want to get all the records with 'time' later than a given time in this function:

function selectData(start_date, num_of_records, callback)
{
    sensorData.findAll({
        limit: num_of_records, 
        where: [time, '???'], <====== How to specify the condition here?
        order: 'time'}).success(callback);
}

I can't find any thing on Sequelize's website about 'date' type based query statement.

Any idea?

like image 914
user2993837 Avatar asked Oct 01 '22 21:10

user2993837


1 Answers

Just like Soumya says, you can use $gt, $lt, $gte or $lte with a date:

model.findAll({
  where: {
    start_datetime: {
      $gte: new Date()
    }
  }
})
like image 199
Evan Siroky Avatar answered Oct 18 '22 08:10

Evan Siroky