Your destination for complete Tech news

How to Filter Collection by Date Range in MongoDB Compass?

1.15K 0
< 1 min read

While using MongoDB Compass, you can easily filter the documents by date ranges using a query object. Let’s assume your field name is timestamp and is stored as an ISODate (MongoDB Date type), the filter would look like the following.

{
  "timestamp": {
    "$gte": { "$date": "2025-08-15T00:00:00.000Z" },
    "$lte": { "$date": "2025-09-18T23:59:59.999Z" }
  }
}

Explanation:

  • $gte In MongoDB query, gte stands for Greater than or equal to
  • $lte in MongoDB query, lte stands for less than or equal
  • $date ensures that the Compass interprets the string as a valid date.

You can also directly pass ISODate to in place of $date, so the new query would look like the following.

{
  "timestamp": {
    "$gte": ISODate("2025-08-15T00:00:00Z"),
    "$lte": ISODate("2025-09-18T23:59:59Z")
  }
}

You can also make use of $gte or $lte separately if you want to filter out data .

For Example:
If you want to filter data only after a certain date, you can use the following. This query will filter out the data after the 2025-08-15

{
  "timestamp": {
    "$gte": ISODate("2025-08-15T00:00:00Z")
  }
}

Similarly, if you want to fetch the data upto some date, you can use the following. This query will filter out the data till 2025-09-18

{
  "timestamp": {
    "$lte": ISODate("2025-09-18T23:59:59Z")
  }
}

This way, you can easily filter out the data in MongoDB Compass Query. But always make sure that the type of the field when making the query. If the filed type of timestamp is not a date object, this will not work.