Category Archives: MongoDB

How to search a MongoDB database for entries between two dates or times

So you have a database in MongoDB with a collection. (I presume you know what that all means, otherwise you may be on the wrong page!)

If each of the documents in your collection contains a timestamp, then you can search for all documents between two dates or times. The following example searches for all documents with timestamps between 2pm and 8pm on the 19th Nov 2013:

> db.collection.find({ timestamp: { $gte:ISODate("2013-11-19T14:00:00Z"), $lt: ISODate("2013-11-19T20:00:00Z") } })

This will theĀ timestamp field to have an index, otherwise the result will take a very long time to compute.