MongoDB Date Comparison Query Examples

In this guide, I will show you various examples of date comparison based queries in MongoDB. If you use MongoDB in your projects, I highly recommend bookmarking this page as a quick reference for date comparison queries.

For the following MongoDB query examples, I will use a collection representing products created in an e-commerce system. Since the examples are focused on date comparison, our product collection will be a simple one consisting only of these fields - name, price, createddate, updateddate. By running the following command on MongoDB shell, we will insert few records into the product collection. Note that the date fields also contain time.

db.products.insert({ name: "PS5", 
    price: 400, createddate: ISODate("2022-01-10T08:30:00.000"), 
    updateddate: ISODate("2022-01-15T08:30:00.000")} )

db.products.insert( { name: "XBox X", 
    price: 320, createddate: ISODate("2022-02-15T18:30:00.000"), 
    updateddate: ISODate("2022-03-15T20:30:00.000")} )

db.products.insert( { name: "Nintendo Switch", 
    price: 220, createddate: ISODate("2022-02-10T14:30:00.000"), 
    updateddate: ISODate("2022-03-11T22:30:00.000")} )

MongoDB supports find() command for queries. Alternatively you can also use the powerful MongoDB aggregation pipeline for queries. For each query requirement, I will provide both find() query and aggregation pipeline query. However I recommend learning aggregation queries as it is much more powerful than find(). Also note that the following queries are tested on MongoDB version 4.4.

How to Find Documents Between Two Dates in MongoDB

The following query returns products created between 1st January 2022 and 10th February 2022. Note that in this case we are not interested in the time for the dates compared. Following is the find() query to find documents within 1st January 2022 and 10th February 2022,

db.getCollection('products').find(
    {createddate:{$gt:ISODate("2022-01-01"),
    $lt:ISODate("2022-02-10")}})

Following is the aggregate() pipeline query,

db.getCollection('products').aggregate(
    [{"$match" : {"createddate" : {"$gt":ISODate("2022-01-01"), 
    "$lt":ISODate("2022-02-10") } }}]);

The above queries return 1 record from the sample data we have added. Please note that when time is not specified, it is assumed to be 12AM UTC time and hence the record with date 2022-02-10T14:30:00.000 is not returned in the query.

The following find() and aggregation queries demonstrate the use of time in ISODate() and also how to filter queries with additional conditions. We want only PS5 products to be returned within the date range.

db.getCollection('products').find(
    {name:"PS5",
    createddate:{$gt:ISODate("2022-01-01T10:10:40"),
    $lt:ISODate("2022-02-10T22:20:01")}});
db.getCollection('products').aggregate([
    {"$match" : {"name":"PS5"}},
    {"$match" : {"createddate" : {"$gt":ISODate("2022-01-01"), 
    "$lt":ISODate("2022-02-10") } }}
]);

How to Find Documents Using Month or Year Comparison in MongoDB

Sometimes you may want to find all the documents where one of the date fields matches a specific month or year. Following aggregate query uses date expression operator to find all the products updated in the month of March. This query first adds a virtual field called month extracting the month field from the updateddate column. It is then used in the match pipeline.

db.getCollection('products').aggregate([
    {$addFields: {  "month" : {$month: '$updateddate'}}},
    {$match: { month: 3}}]);

Following find() query returns all products updated in the month of March,

db.getCollection('products').find({
  $expr: {
        "$eq": [{"$month": "$updateddate"},3]
  }
})

You can filter on the year field of updated date by replacing $month with $year.