5

I want to get the new rows from a model. The rows that have been created after a given date. The query should be very easy:

where updatedAt >= givenDate

But it's not working :(

My code:

        // Date from client
    var lastDate = req.param("date");

    // Parse date with moment? I have tried with and without this.
    lastDate = moment(lastDate).format('YYYY-MM-DD hh:mm:ss');

    var query = Message.find().where({
        updatedAt: {
            '>=':   lastDate
        }
    });

    query.exec(function(err, messages) {
        // I get ALL the messages, :(
        res.send(messages);
    });

Thanks in advance.

Cœur
  • 37,241
  • 25
  • 195
  • 267
josec89
  • 1,932
  • 1
  • 16
  • 19
  • possible duplicate of [How do I compare datetime for sails.js models](http://stackoverflow.com/questions/23527136/how-do-i-compare-datetime-for-sails-js-models) – sgress454 Jun 20 '14 at 23:11

1 Answers1

7

Solved.

I created a Date instance and passed that to the where clause:

// Date from client: Date in MS (new Date().getTime())
    var lastDate = req.param("date");

    // Create date
    lastDate = new Date(lastDate);


    var query = Message.find().where({
        updatedAt: {
            '>=': lastDate
        }
    });

    query.exec(function(err, messages) {
        // I get ALL the messages, :(
        res.send(messages);
    });
josec89
  • 1,932
  • 1
  • 16
  • 19
  • 1
    Hey good option but what about time? Have tried doing the same but just with Time not with the date? I can't do this, even I don't know how to send a value like `19:11:23` and parse it to be stored on the mongoldb database. Any idea how to solve it? – alexventuraio Dec 16 '15 at 18:15