MongoDB – Find the Objects between two dates

MongoDB to Find the Objects between two dates

Today this article will see how to write a query in MongoDB to find the objects between two dates. We shall see examples with dates using timestamp fields (if available) or using Mongo-defined timestamp available via _Id as objectId.

Today in this article, we will cover below aspects,

Approaches to get the records between two dates,

  • Using the dates field if exist
  • Using MongoDB _Id ObjectId field

We will see greater than or less than date queries using CLI or Compass UI.

Getting started

Using the Custom Date field (if available)

Do you follow schema-driven development?

If your schema already has any date or timestamp field then you can use the same to query to get all the records. Here below is a sample schema or document we shall use where I have a custom date field called “DateAdded

Example:

{
  "_id": "62c5e3f602514a766441e2ef",
  "UserId": 999999,
  "DateAdded": "2021-01-01T05:00:00.000Z",
  "Books": [
    {
      "book_name": "book_name1",
      "price": 88,
      "category": [
        {
          "key": "Test1",
          "value": "60"
        }
      ]
    },
    {
      "book_name": "book_name2",
      "price": 50,
      "category": [
        {
          "key": "test3",
          "value": "60"
        },
        {
          "key": "test4",
          "value": "100"
        }
      ]
    }
  ]
}

Find objects using CLI or Compass UI

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

Find the MongoDB Documents when the Date is a greater specified date

Command

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

 

Find the MongoDB Documents date is less than the specified date

Command

{ <Field Name>: { $lt:ISODate('Date here') } }
MongoDB Find the Objects between two dates

For more details on other queries, please refer below article

Find the Objects using the _Id field i.e ObjectId

This is another approach typically useful when you don’t have any timestamp or date field in your schema.

As we know, MongoDB uses the _id field as ObjectId. This Id has few behavioral characteristics.

It is a 4-byte timestamp is a UNIX timestamp.

Timestamp represents the ObjectId’s creation timestamp when a document is inserted into Mongo collection first time, measured in seconds since the Unix epoch. for more details, please visit here.

Command

{ <field Name>: { $gt: ObjectId.fromDate( new ISODate("Date here"))}}

OR

 <field Name>: { $lt: ObjectId.fromDate( new Date("Date here"))}} 

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.



Leave a Reply

Your email address will not be published. Required fields are marked *