Almog
Almog

Reputation: 2837

Between dates query in MongoDB

I'm trying query between two dates in MongoDB but I'm not getting anything back but I do have the correct data. I'm thinking that the date format might be wrong. Is it correct?

This is the query that I'm running

  { '$gte': Sun Feb 01 2015 02:00:00 GMT+0200 (IST),
      '$lt': Tue Mar 03 2015 02:00:00 GMT+0200 (IST) }

and my JS query code looks like this

query.created = {$gte:new Date(this.props.startDate), $lt:new Date(this.props.endDate)};

Upvotes: 2

Views: 24948

Answers (3)

user7110739
user7110739

Reputation:

db.collection.find({
    created_at: {
        $gte: ISODate("2015-02-01T00:00:00.000Z"),
        $lt: ISODate("2015-03-03T00:00:00.000Z")
    }
})

Upvotes: 2

Dave
Dave

Reputation: 433

I use twix.js and publish my collection for responses in an array that match my date-range condition.

Upvotes: 0

chomsky
chomsky

Reputation: 300

Mongo recognizes the ISODate type. It can be used to make common date-related queries (like the one you're trying to do).

So it should be something like this:

{ '$gte': ISODate(01-02-2015T02:00:00Z),
  '$lt': ISODate(03-03-2015T02:00:00Z) }

Please read further this documentation for more details.

Also, check out this link to the oficial online courses, there is a section where it cover your problem.

Upvotes: 5

Related Questions