MongoDB Date query to get records between two dates

MongoDB Date query to get records between two dates

Today, in this article will see how to write a MongoDB Date query to get records based on timestamp or date range.

We can easily find the records between the two given dates. These queries are effective means to perform efficient searches.

We will cover the below aspects in today’s article,

We already looked at a simple way of adding or updating a new field to the document in our previous MongoDB sample series.

We shall see examples for the MongoDB Date Range filter including a date greater than or less than time or date assuming we already have a date-specific field in the database.

If you don’t have a date or any timestamp field in the document, you can still use another approach using _Id an ObjectID field to get the records between two dates.

Please visit the below article for the same,

Getting started

Here below is a sample schema or document we shall use for the date range query,

MongoDB query Date

MongoDB Find objects between Two dates

Here we will be using the below query to get the documents between two dates in MongoDB Collection.

Mongo Date greater than query (‘gt’) – ISO Date

The below query gives us all the records matching the Date greater than the query

Command

{ <Field Name>: { $gt:ISODate('Date here') } }

 

Example

db.collection.find({ "DateAdded": { $gt: ISODate("2023-05-01T00:00:00Z") } })


MongoDB query date without timestamp

The below query gives us all the records using greater than the query with date only


Example

db.collection.find({ "DateAdded": { $gt: ISODate("2023-05-01") } })


MongoShell query

mongodb date query

Mongo Date Less than query (‘$lt)

The below query gives us all the records using field value where it matches with the less than the given date.

Command

{ <Field Name>: { $lt:ISODate('Date here') } }

Example

db.collection.find({ "DateAdded": { $lt: ISODate("2023-05-01T00:00:00Z") } })


MongoDB query – For records between greater than ($gte) the date and less than ($lte) dates

The below query gives us all the records using field value where it matches with the less than and greater than the given date.

Command

{  <Field name> : { $gt:ISODate('Date Here'), $lt:ISODate('Date here')  }  }


Examples

db.collection.find({ DateAdded : { $gt:ISODate('2019-09-18T21:07:42.313+00:00')}, { $lt:ISODate('2020-09-18T21:07:42.313+00:00')}})

Mongo Atlas UI

MongoDB Date Time query greater or less than examples

Mongo shell

mongodb-query between two dates,
mongodb query greater than and less than date,
mongodb query date greater than,
mongodb query greater than and less than date in java,
<a href=

What if there is no Date field in Mongo Schema?

You may be wondering if there is a way to perform a search query based on a date even if there is no Date field in Mongo Schema.

It’s possible but with a few limitations.

Please visit the article for more details…

What is the difference between $gt and $gte or $lt and $lte in MongoDB?

The above commands are very simple and easy to understand. There are small differences between these commands.

$gte

$gte  = greater than or equal to i.e > =

$gt

$gt  = greater than i.e > 

$lte

lte  = less than or equal to i.e < =

$lt

lt  = less than i.e < 

TIP – IF you need to use Data range query often then is recommanded to indexed the dates fields.

Other References

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.