I want to query Mongo based on timestamp. Follwing is the field in mongo.
"timestamp" : "2016-03-07 11:33:48"
Books is the collection name and below is my query for time period of 1 minute:
db.Books.find({"timestamp":{$gte: ISODate("2016-03-07T11:33:48.000Z"), $lt: ISODate("2016-03-07T11:34:48.000Z")}})
Also is there any alternative like I don't have to give greater and lower limit on timestamp. But query based time interval mentioned. Something like, if present timestamp is TS = "2016-03-07T11:33:48.000Z" then query should be between TS and TS + 1 minute rather than explicitly mentioning timestamp. Something like adding 1 minute to present timestamp
Making sure that all your applications create and update MongoDB timestamps is going to be the hardest part of this process; MongoDB has no automatic timestamp support, so every one of your applications is going to have to make sure they do one of two things when working with the database: write a createdAt field, or ...
MongoDB stores times in UTC by default, and will convert any local time representations into this form. Applications that must operate or report on some unmodified local time value may store the time zone alongside the UTC timestamp, and compute the original local time in their application logic.
db.Books.find({"timestamp":{$gte: "2016-03-07 11:33:48", $lt: "2016-03-07 11:34:48"}})
ISODate is not required here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With