anil
anil

Reputation: 853

Mongodb date with time is not working properly

I am a newbie to mongodb. I am writing a query to fetch documents between two dates with time. If the dates range falls in past days then giving results correctly (like 2017-01-01T00:00:00.000Z to 2017-02-01T23:59:59.999Z). If the date range falls within today then it is giving records wrongly (like 2017-02-20T00:00:00.000Z to 2017-02-20T16:59:59.999Z). Below is the query

db.collection.count({"date1":{$gte:ISODate("2017-01-01T00:00:00.000Z")},"date1":{$lte:ISODate("2017-02-01T23:59:59.999Z")}});

it gives count as 221. Now for today:

db.collection.count({"date1":{$gte:ISODate("2017-02-20T00:00:00.000Z")},"date1":{$lte:ISODate("2017-02-20T16:59:59.999Z")}});

it gives me count as 56. If I add a new record after this(2017-02-20T16:59:59.999Z) time then the same query giving me count as 57. Also, I tried with $and operator but the same result.

Kindly help me out.

Thanks in advance.

Upvotes: 2

Views: 635

Answers (1)

sandeep rawat
sandeep rawat

Reputation: 4957

try below code.

db.collection.count({"date1":{$gte:ISODate("2017-02-20T00:00:00.000Z") ,$lte:ISODate("2017-02-20T16:59:59.999Z")}});

Upvotes: 2

Related Questions