0

I am new to Mongodb. I want to find those objects stored in mongodb whose receivedOn date is greater than a particular date. My object structure is :

{
    "_id" : ObjectId("591313fa79a7f2826cdfcdbd"),
    "uuid" : "849cf178-bf19-4a32-bda8-b754551c57f0",
    "status" : "SENT",
    "receivedOn" : ISODate("2017-05-10T18:51:58.893+05:30"),
    "scheduledOn" : ISODate("2017-05-10T18:51:58.893+05:30"),
    "deliveredOn" : ISODate("2017-05-10T18:52:02.628+05:30")
}

I am using Jongo driver for querying. My query looks like:

collection.find("{\"receivedOn\": {$gte : #}}", javaDate);

I am getting no results from this query but there are documents in collection which should have been returned. What's wrong with my query?

Vivek Mangal
  • 532
  • 1
  • 8
  • 24

2 Answers2

0
db.CollectionName.find({"receivedOn" : new ISODate("2017-05-10T18:51:58.893+05:30") });
Prabhav
  • 447
  • 3
  • 17
0

As you are using java,util.Date, there could be timezone issues when Date object is converted to json. Java might be adding/subtracting hours to your date based on timezone. Ideally while storing dates, they should be in UTC format, and while retrieving, they should be in UTC.

adi
  • 304
  • 2
  • 11
  • Even if I use same timezone, I am not getting result. Is there something with my query? If i query using ISODate in mongo shell , I get correct resuts. Is mongo date type compatible with java.util.Date? – Vivek Mangal May 11 '17 at 09:54
  • How are you ensuring that it is same time zone? can you please post more code around it? – adi May 11 '17 at 10:03