How to match date with MongoDB $match?

To match date in MongoDB, use $match operator within the aggregation pipeline. The $match stage filters documents based on date criteria using comparison operators like $gte, $lte, or $eq.

Syntax

db.collection.aggregate([
    {
        $match: {
            "dateField": {
                "$operator": ISODate("YYYY-MM-DDTHH:mm:ss.sssZ")
            }
        }
    }
]);

Sample Data

db.demo491.insertMany([
    {"ShippingDate": ISODate("2020-01-10")},
    {"ShippingDate": ISODate("2020-02-21")},
    {"ShippingDate": ISODate("2020-03-23")},
    {"ShippingDate": ISODate()}
]);
{
    "acknowledged": true,
    "insertedIds": [
        ObjectId("5e849a09b0f3fa88e22790be"),
        ObjectId("5e849a0eb0f3fa88e22790bf"),
        ObjectId("5e849a1db0f3fa88e22790c0"),
        ObjectId("5e849a28b0f3fa88e22790c1")
    ]
}

Display all documents from the collection ?

db.demo491.find();
{ "_id": ObjectId("5e849a09b0f3fa88e22790be"), "ShippingDate": ISODate("2020-01-10T00:00:00Z") }
{ "_id": ObjectId("5e849a0eb0f3fa88e22790bf"), "ShippingDate": ISODate("2020-02-21T00:00:00Z") }
{ "_id": ObjectId("5e849a1db0f3fa88e22790c0"), "ShippingDate": ISODate("2020-03-23T00:00:00Z") }
{ "_id": ObjectId("5e849a28b0f3fa88e22790c1"), "ShippingDate": ISODate("2020-04-01T13:42:00.090Z") }

Example: Match Dates Greater Than or Equal

Find documents with ShippingDate greater than or equal to February 24, 2020 ?

db.demo491.aggregate([
    {
        $match: {
            "ShippingDate": {
                "$gte": ISODate("2020-02-24T18:10:11.000Z")
            }
        }
    }
]);
{ "_id": ObjectId("5e849a1db0f3fa88e22790c0"), "ShippingDate": ISODate("2020-03-23T00:00:00Z") }
{ "_id": ObjectId("5e849a28b0f3fa88e22790c1"), "ShippingDate": ISODate("2020-04-01T13:42:00.090Z") }

Date Range Operators

  • $eq − Exact date match
  • $gte − Greater than or equal to date
  • $lte − Less than or equal to date
  • $gt − Greater than date
  • $lt − Less than date

Conclusion

Use $match in aggregation pipelines to filter documents by date criteria. Combine with date comparison operators like $gte and $lte to create effective date range queries.

Updated on: 2026-03-15T03:16:56+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements