简体   繁体   中英

Querying by date in mongo db

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?

db.CollectionName.find({"receivedOn" : new ISODate("2017-05-10T18:51:58.893+05:30") });

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM