Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to work Date query with ISODate in MongoDB?
Use date comparison operators like $gte, $lt, and $between along with ISODate() to work with date queries in MongoDB. ISODate provides a standardized way to store and query dates in UTC format.
Syntax
db.collection.find({
"dateField": {
"$gte": ISODate("YYYY-MM-DDTHH:mm:ssZ"),
"$lt": ISODate("YYYY-MM-DDTHH:mm:ssZ")
}
});
Sample Data
Let us create a collection with sample documents containing date fields ?
db.dateDemo.insertMany([
{
"StudentName": "John",
"StudentAge": 26,
"AdmissionDate": new ISODate("2013-06-07")
},
{
"StudentName": "Alice",
"StudentAge": 24,
"AdmissionDate": new ISODate("2015-03-15")
},
{
"StudentName": "Bob",
"StudentAge": 22,
"AdmissionDate": new ISODate("2020-01-10")
}
]);
{
"acknowledged": true,
"insertedIds": [
ObjectId("5c8a65799064dcd4a68b70ea"),
ObjectId("5c8a65799064dcd4a68b70eb"),
ObjectId("5c8a65799064dcd4a68b70ec")
]
}
Display all documents to verify the data ?
db.dateDemo.find().pretty();
{
"_id": ObjectId("5c8a65799064dcd4a68b70ea"),
"StudentName": "John",
"StudentAge": 26,
"AdmissionDate": ISODate("2013-06-07T00:00:00Z")
}
{
"_id": ObjectId("5c8a65799064dcd4a68b70eb"),
"StudentName": "Alice",
"StudentAge": 24,
"AdmissionDate": ISODate("2015-03-15T00:00:00Z")
}
{
"_id": ObjectId("5c8a65799064dcd4a68b70ec"),
"StudentName": "Bob",
"StudentAge": 22,
"AdmissionDate": ISODate("2020-01-10T00:00:00Z")
}
Example 1: Find Students Admitted After a Specific Date
Query students admitted on or after June 7, 2013 ?
db.dateDemo.find({
"AdmissionDate": {
"$gte": ISODate("2013-06-07T00:00:00Z")
}
}).pretty();
{
"_id": ObjectId("5c8a65799064dcd4a68b70ea"),
"StudentName": "John",
"StudentAge": 26,
"AdmissionDate": ISODate("2013-06-07T00:00:00Z")
}
{
"_id": ObjectId("5c8a65799064dcd4a68b70eb"),
"StudentName": "Alice",
"StudentAge": 24,
"AdmissionDate": ISODate("2015-03-15T00:00:00Z")
}
{
"_id": ObjectId("5c8a65799064dcd4a68b70ec"),
"StudentName": "Bob",
"StudentAge": 22,
"AdmissionDate": ISODate("2020-01-10T00:00:00Z")
}
Example 2: Date Range Query
Find students admitted between 2013 and 2016 ?
db.dateDemo.find({
"AdmissionDate": {
"$gte": ISODate("2013-01-01T00:00:00Z"),
"$lt": ISODate("2016-01-01T00:00:00Z")
}
}).pretty();
{
"_id": ObjectId("5c8a65799064dcd4a68b70ea"),
"StudentName": "John",
"StudentAge": 26,
"AdmissionDate": ISODate("2013-06-07T00:00:00Z")
}
{
"_id": ObjectId("5c8a65799064dcd4a68b70eb"),
"StudentName": "Alice",
"StudentAge": 24,
"AdmissionDate": ISODate("2015-03-15T00:00:00Z")
}
Date Range Operators
| Operator | Description | Example |
|---|---|---|
$gte |
Greater than or equal to | {"date": {"$gte": ISODate("2020-01-01")}} |
$gt |
Greater than | {"date": {"$gt": ISODate("2020-01-01")}} |
$lte |
Less than or equal to | {"date": {"$lte": ISODate("2020-12-31")}} |
$lt |
Less than | {"date": {"$lt": ISODate("2020-12-31")}} |
Conclusion
MongoDB's ISODate() combined with comparison operators like $gte and $lt provides powerful date querying capabilities. Always use the ISO 8601 format for consistent date handling across different time zones and applications.
Advertisements
