When working with time-sensitive data in MongoDB, you often need to retrieve documents that fall within a specific date window. MongoDB stores dates in BSON format as 64-bit integers representing milliseconds since the Unix epoch, which makes date comparisons precise and consistent across queries.
This article covers how to filter a collection by a date field using MongoDB’s comparison operators, with examples for inclusive and exclusive date ranges.
MongoDB provides four comparison operators for date fields. Choosing the right combination determines whether your range boundaries are included in the results.
$gte – greater than or equal to (inclusive lower boundary)$lte – less than or equal to (inclusive upper boundary)$gt – greater than (exclusive lower boundary)$lt – less than (exclusive upper boundary)Use $gte and $lte when you want results that include the boundary dates themselves. Use $gt and $lt when you want to exclude them.
The db.collection.find() method accepts a query object as its first parameter. To filter by a date range, pass a comparison object against your date field. The example below queries a cakeSales collection for all orders placed before a specific date.
db.cakeSales.find( { orderDate: { $lt: ISODate("2021-02-25T10:03:46.234Z") } } )
To filter between two dates, combine two operators on the same field. The query below returns all documents from the bios collection where the birth field falls after 1 January 1940 and before 1 January 1960, excluding boundary dates.
db.bios.find( { birth: { $gt: new Date('1940-01-01'), $lt: new Date('1960-01-01') } } )
To make the boundaries inclusive, replace $gt and $lt with $gte and $lte. The query below returns all documents from a sales collection where saleDate falls on or between 1 January 2016 and 2 January 2016 UTC.
{ saleDate: { $gte: { $date: "2016-01-01T00:00-00:00" }, $lte: { $date: "2016-01-02T00:00-00:00" } } }
You can also query system collections in the same way. The example below finds profiled operations that ran within a 40-minute window, which is useful when investigating performance during a specific period.
db.system.profile.find( {
ts : {
$gt: new ISODate("2012-12-09T03:00:00Z"),
$lt: new ISODate("2012-12-09T03:40:00Z")
}
} ).pretty()
All date values in these examples are expressed in UTC. MongoDB stores dates in UTC by default, so if your application handles multiple time zones, convert dates to UTC before storing or querying them to avoid mismatched results.
Without an index on your date field, MongoDB performs a full collection scan for every date range query. On large collections this becomes slow. Creating an index on the date field tells MongoDB to scan only the relevant portion of the data.
Run the following command to create an ascending index on a date field in your collection, replacing events and date with your actual collection and field names.
db.events.createIndex( { date: 1 } )
Once the index exists, MongoDB uses it automatically for any query that filters on that field. You do not need to modify your find() queries.
You can now filter MongoDB documents by date range using $gte, $lte, $gt and $lt operators inside a find() query. Choosing between inclusive and exclusive operators gives you precise control over which boundary dates appear in your results.
For related database topics, see our guides on creating a MySQL database in cPanel and importing SQL over the command line. If you are running MongoDB on a VPS, our guide on connecting to a database with an SSH tunnel covers how to access your database securely from a remote machine.
Launch your website with our reliable cPanel hosting with unlimited bandwidth and expert support.
Get cPanel Hosting