Search code examples
javascriptmongodbsails.jssails-mongo

Sails-mongo get result between two dates


I am working on sails js with mongodb with sails-mongo adapter , My problem is i cant fetch the result between two dates using sails-mongo.

So is there any solution for retrieve this data, actually i want to get total count of each day day's entries (Last 15 days).

So please share if you know anything or any alternative way to come out from this problem.

-- Thanks - ND


Solution

  • Simply use condition in single object in WHERE or FIND or COUNT

    .where({ "createdAt" : { ">" : new Date(start), "<" : new Date(end) }})

    .find({ "createdAt" : { ">" : new Date(start), "<" : new Date(end) }})

    .count({ "createdAt" : { ">" : new Date(start), "<" : new Date(end) }})

    See This example

        var start = moment('12-06-2014').startOf('day');
        var end = moment('12-06-2014').endOf('day');
    
        User.find().where({ "createdAt" : { ">" : new Date(start), "<" : new Date(end) }})
                .exec(function (err, user) {
    
                    if (err)
                        return res.json(Res.toJson(false, err, 'Error'))
    
                    return res.json(Res.toJson(true, user, 'User'))
                });