How do query dates by using ISODate in MongoDB? In this article, we will explore different examples of querying dates in MongoDB using the ISODate()
function. The ISODate()
function creates a Jav.Script
Date object with the specified date and time in ISO format (i.e., “yyyy-mm-ddThh:mm:ssZ
“), which is the format used by MongoDB to store dates.
Here, we will use the following documents from the collection student
to explain ISODate()
#create collection
db.student.insertMany([
{
"_id": 1,
"Name": "Lark Messy",
"Admission_date": ISODate("2022-05-30T00:00:00.000Z")
},
{
"_id": 2,
"name": "Harry Peter",
"Admission_date": ISODate("2019-12-21T00:00:00.000Z")
},
{
"_id": 3,
"name": "Bella Joe",
"Admission_date": ISODate("2023-01-16T00:00:00.000Z")
},
{
"_id": 4,
"name": "Alice Mark",
"Admission_date": ISODate("2020-07-11T00:00:00.000Z")
},
{
"_id": 5,
"name": "Elijah David",
"Admission_date": ISODate("2021-11-26T00:00:00.000Z")
}
]
)
1. Query Documents with specified ISODate
Use ISODate() function to query a date object in MongoDB, this function takes the data as a string and returns the date object, If your document has a date represented as ISODate then you have to use this function to retrieve values based on dates.
Here in the following query, we have specified the ISODate()
function with the object date to the date field Admission_date
. This will match the date object with the dates in the collection student
and return the documents created on that date.
# Usage of ISODate() function
db.student.find({ Admission_date: ISODate("2022-05-30T00:00:00.000Z") })
In the result, the document matched with the ISODate is obtained.
2. Query where Dates are Greater than Specified ISODate
However, the ISODate function of MongoDB is also used to query the documents where the dates are greater than the specified ISODate.
For this, we have to use the $gt
operator and assigned the ISODate function with the date object inside it. Consider the following query where we have retrieved the dates from the collection student
which satisfies the given condition.
The $gt
operator here compares the date given to the ISODate function with the date field Admission_date
of the collection and returned the documents greater than the specified ISODate.
# Usage of ISODate() function with $gt operator
db.student.find({ Admission_date: { $gt: ISODate('2019-12-21') } })
The output here shows the documents which are greater than the given ISODate.
3. Query where Dates are less than the Specified ISODate
Conversely, to query for documents before a specific date, we can use the $lt
operator in our query. For example, to find all documents created before the specified ISODate, we can run the following query where the $lt
operator compares the date from the Admission_date
field with the date assigned to the ISODate function.
As a result, this will return all documents where the Admission_date
field is less than the date in the ISODate function.
# Usage of ISODate() function with $lt operator
db.student.find({ Admission_date: { $lt: ISODate('2023-01-16') } })
The output yielded the following dates which are less than the given to the ISODate function.
4. Find Documents between two specific Dates
Next, we have a case where we find the documents between two specific ISODate by using the $gte
and $lte
operators in a single query.
Considering the following query, the $gte
, and $lte
operators are used to specify a range of dates using the ISODate function and returned the dates from the Admission_date
field stratified the date ranges.
# Usage of ISODate() function with $gte and $lte operators
db.student.find({ Admission_date: { $gte: ISODate('2019-12-21'), $lte: ISODate('2022-05-30') } })
The output displayed the dates obtained after executing the above query.
5. Update Dates with the ISODate in MongoDB
Additionally, we can also use ISODate with the update method to modify the documents based on date criteria.
Here’s an example of using the ISODate with the update method to set a new value newAdmission_date
for a field in documents that have a date field Admission_date
less than a specified date set in the ISODate function. The multi
option here updates all matching documents, rather than just the first one.
# Usage of Update() function
db.student.update(
{ Admission_date: { $lt: ISODate("2021-11-26T00:00:00Z") } },
{ $set: { newAdmission_date: "2023-02-06T00:00:00Z" } },
{ multi: true }
)
The documents are updated with the new dates in the following output.
6. Query to Delete Dates with the ISODate in MongoDB
Similarly, the ISODate can also be utilized with the delete method to remove the document which satisfies the given date criteria.
In the query below, we have specified the criteria for the documents we want to delete using the $lt
operator, which compares the value of the date field to a specified ISODate. The deleteMany
method is used to delete all matching documents.
# Usage of deleteMany() function
db.student.deleteMany(
{ Admission_date: { $lt: ISODate("2023-01-06T00:00:00Z") } }
)
The output displayed only one document which means all the other documents are less than the set ISODate.
7. Conclusion
In conclusion, we have walked through the different cases to query dates by using the ISODate function in MongoDB. Note that the dates specified in the query are in the UTC zone. If the dates are in a different time zone, we may need to adjust them accordingly.