Mongoose: query date range

Viewed 14120

Im trying to get all documents within a specific date range. Im using MomentJS and Mongoose in my Express router. But the result set is empty:

router.route("/bookings")

    // GET
    .get(function(req, res) {
        var start,
            end;

        // set time zone
        moment().tz("Europe/Copenhagen").format();

        start = moment(req.query.start * 1000).toDate();
        end = moment(req.query.end * 1000).toDate();

        Booking.
            find({
                startDate: {
                    $gte: start,
                    $lte: end
                }
            }).
            exec(function(err, bookings) {
                if (err) handleErr(res, err);

                console.log(err);
                console.log(bookings);

                res.json(bookings);
            });
    })

The route receives start and end dates as UNIX TIMESTAMPS (also from MomentJS on the client side).

If I query from the mongo shell I get the correct documents:

db.bookings.find({startDate: { $gte: "2016-02-29T00:00:00+01:00", $lte: "2016-03-06T23:59:59+01:00" } }, {startDate: 1})

If I console.log the dates, I get EXACTLY the format as when I query the database.

I tried with/without UNIX * 1000 but no luck there...

What am I doing wrong?

0 Answers
Related