Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query mongo on timestamp

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

like image 473
dsl1990 Avatar asked Mar 07 '16 19:03

dsl1990


People also ask

Does MongoDB have 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 ...

How is Timestamp stored in MongoDB?

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.


1 Answers

db.Books.find({"timestamp":{$gte: "2016-03-07 11:33:48", $lt: "2016-03-07 11:34:48"}})

ISODate is not required here

like image 200
dsl1990 Avatar answered Oct 02 '22 15:10

dsl1990