0
0
MongodbHow-ToBeginner · 2 min read

MongoDB Query to Find Documents by Date Range

Use the query { dateField: { $gte: ISODate("start_date"), $lte: ISODate("end_date") } } to find documents where the date is between the start and end dates inclusive.
📋

Examples

Input{ dateField: { $gte: ISODate("2023-01-01"), $lte: ISODate("2023-01-31") } }
Output[{ "_id": 1, "dateField": ISODate("2023-01-10") }, { "_id": 2, "dateField": ISODate("2023-01-20") }]
Input{ dateField: { $gte: ISODate("2023-05-01"), $lte: ISODate("2023-05-01") } }
Output[{ "_id": 3, "dateField": ISODate("2023-05-01") }]
Input{ dateField: { $gte: ISODate("2024-01-01"), $lte: ISODate("2024-01-31") } }
Output[]
🧠

How to Think About It

To find documents in a date range, think of filtering dates that are greater than or equal to the start date and less than or equal to the end date. MongoDB uses $gte for 'greater than or equal' and $lte for 'less than or equal' to compare dates stored in ISODate format.
📐

Algorithm

1
Get the start date and end date as input.
2
Use the $gte operator to filter documents with dates on or after the start date.
3
Use the $lte operator to filter documents with dates on or before the end date.
4
Combine both conditions in a single query on the date field.
5
Return all documents matching the date range.
💻

Code

mongodb
db.collection.find({
  dateField: {
    $gte: ISODate("2023-01-01T00:00:00Z"),
    $lte: ISODate("2023-01-31T23:59:59Z")
  }
})
Output
[ { "_id": ObjectId("..."), "dateField": ISODate("2023-01-10T12:00:00Z") }, { "_id": ObjectId("..."), "dateField": ISODate("2023-01-20T08:30:00Z") } ]
🔍

Dry Run

Let's trace the query for dates between 2023-01-01 and 2023-01-31.

1

Input Dates

Start date = 2023-01-01T00:00:00Z, End date = 2023-01-31T23:59:59Z

2

Filter Documents

Check each document's dateField to see if it is >= start date and <= end date

3

Return Matches

Documents with dateField 2023-01-10 and 2023-01-20 are returned

Document _iddateFieldMatches Range?
12023-01-10T12:00:00ZYes
22023-01-20T08:30:00ZYes
32023-02-01T00:00:00ZNo
💡

Why This Works

Step 1: Use $gte and $lte

The $gte operator selects documents with dates greater than or equal to the start date, and $lte selects those less than or equal to the end date.

Step 2: Dates stored as ISODate

Dates in MongoDB are stored as ISODate objects, which allow accurate comparison using these operators.

Step 3: Combined condition filters range

Combining $gte and $lte in one query filters documents whose dateField falls within the inclusive range.

🔄

Alternative Approaches

Using $and operator
mongodb
db.collection.find({
  $and: [
    { dateField: { $gte: ISODate("2023-01-01") } },
    { dateField: { $lte: ISODate("2023-01-31") } }
  ]
})
This is more verbose but functionally equivalent; useful if combining multiple conditions.
Using $expr with aggregation operators
mongodb
db.collection.find({
  $expr: {
    $and: [
      { $gte: ["$dateField", ISODate("2023-01-01")] },
      { $lte: ["$dateField", ISODate("2023-01-31")] }
    ]
  }
})
Allows more complex expressions but is less readable and slower for simple date range queries.

Complexity: O(n) time, O(k) space

Time Complexity

The query scans documents and compares each dateField, so time grows linearly with the number of documents.

Space Complexity

Space depends on the number of matching documents returned, stored in memory as k.

Which Approach is Fastest?

Using direct $gte and $lte operators is fastest and simplest; $expr adds overhead and is slower.

ApproachTimeSpaceBest For
$gte and $lteO(n)O(k)Simple and efficient date range queries
$and with $gte and $lteO(n)O(k)When combining multiple conditions explicitly
$expr with $andO(n)O(k)Complex expressions but slower performance
💡
Always store dates as ISODate objects to enable accurate range queries with $gte and $lte.
⚠️
Using string dates instead of ISODate objects causes incorrect or no matches in date range queries.