14

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

2 Answers 2

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

ISODate is not required here

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.