dsl1990
dsl1990

Reputation: 1209

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

Upvotes: 13

Views: 34396

Answers (2)

Edward Tan
Edward Tan

Reputation: 954

If someone is searching for how to do this using the MongoDB Atlas GUI's Collections tab interface, this is what one would do. One can put this query string in the "Filter" UI and hit Apply:

Assuming there's a timestamp field named "modified_time" of type "Date" in the schema, one would need to use this as the Filter input:

{ modified_time: {$gt: ISODate('2023-01-01')} }

Note that use "Date" instead of "ISODate" will not work in this case. And doing it without the ISODate() also will not work unlike the other answer, which is when doing it programmatically.

Upvotes: 1

dsl1990
dsl1990

Reputation: 1209

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

ISODate is not required here

Upvotes: 13

Related Questions