Open In App

How to Find Objects Between Two Dates MongoDB

Last Updated : 03 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

MongoDB is a widely used NoSQL database and One common requirement in database operations is to query for objects within a certain date range. In MongoDB, this can be achieved using comparison operators along with the $and operator.

In this article, We will learn about How to Find objects between two dates MongoDB by understanding some operators as defined in the article with the help of examples and output in detail.

Find Objects between two dates in MongoDB

MongoDB stores data as documents. A Database contains a collection, and a collection contains documents and the documents contain data, the document can be also called an object. Some entries may contain the dates for example sales collection with purchase_date as field and date as value. We can find the objects between two dates using the find() method and using the comparison operators in MongoDB. To find the date we require a first date and last date for the comparison

Syntax:

db.collection.find({
"date": {
$gt: new Date("YYYY-mm-dd"),
$lt: new Date("YYYY-mm-dd")
}
})

Explanation:

  • The find() method in MongoDB selects documents in a collection that matches the specified conditions and returns a cursor to the selected documents.
  • MongoDB provides less than and greater than operator for comparison of field values.
  • Date(): It returns the current date in string format.
  • new Date(): Returns a date object. Uses the ISODate() wrapper.
  • new ISODate(): It also returns a date object. Uses the ISODate() wrapper.

Prerequisites

  • $gte Operator in MongoDB: The $gte operator selects documents where the field value is greater than or equal to the specified value.
  • $lte Operator in MongoDB: The $lte operator selects documents where the field value is less than or equal to the specified value
  • $gt Operator in MongoDB:The $gt operator selects documents where the field value is greater than the specified value.
  • $lt Operator in MongoDB: The $lt operator selects documents where the field value is less than the specified value.

Examples of Find objects between two dates MongoDB

To understand How to Find objects between two dates MongoDB we need a collection and some documents on which we will perform various operations and queries. Here we will consider a collection called Customer which contains the information shown below:

(
[
{ c_id: 1, pur_item: "Butter", purchase_date: ISODate("2024-03-19") },
{ c_id: 2, pur_item: "Milk", purchase_date: ISODate("2024-03-26") },
{ c_id: 3, pur_item: "Bread", purchase_date: ISODate("2024-04-02") },
{ c_id: 4, pur_item: "Egg", purchase_date: ISODate("2024-04-11") },
{ c_id: 5, pur_item: "tooth_paste", purchase_date: ISODate("2024-04-20") },
{ c_id: 6, pur_item: "Hat", purchase_date: ISODate("2024-04-25") }
]
);

Example 1: Find the object within the 20th March and 10th April

db.Customer.find({
purchase_date: {
$gte: new Date("2024-03-20"),
$lte: new Date("2024-04-10")
}
});

Output:

Find-Objects-between-two-dates-in-MongoDB-

Find Objects between two dates in MongoDB

Explanantion:

  • In the above example GeeksforGeeks database contains Customer collection.Customer collection contains 6 documents with c_id ,pur_item, purchase_date as the field.
  • We use greater than equal to and less than equal to operator to find the document within the range of 20th March and 10th April.As 2 document lies within the range hence they are returned.

Example 2: Find Objects between two dates in MongoDB using comparison operator

db.Customer.find({
purchase_date: {
$gt: new Date("2024-04-11"),
$lte: new Date("2024-04-30")
}
});

Output:

Find-Objects-between-two-dates-in-MongoDB-using-comparison-operator

Find Objects between two dates in MongoDB using comparison operator

Explanation:

  • In the above example GeeksforGeeks database contains Customer collection.Customer collection contains 3 documents with c_id ,pur_item, purchase_date as the field.
  • We use greater than and less than operator to find the document within the range of 11th April and 30th April.As 2 document lies within the range hence they are returned.

Conclusion

Overall, MongoDB offers powerful features for querying objects within a specific date range. By utilizing comparison operators such as $gte, $lte, $gt, and $lt, along with the $and operator, MongoDB allows for precise and efficient retrieval of documents based on date values. Through examples and detailed explanations, this article has demonstrated how to effectively query objects between two dates in MongoDB. Understanding these concepts and operators is crucial for developers working with date-sensitive data in MongoDB databases.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads