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
Find records on or after a specific date in MongoDB?
To find records on or after a specific date in MongoDB, use the $gte (greater than or equal) operator with ISODate to perform date comparisons.
Syntax
db.collection.find({
"dateField": { $gte: ISODate("YYYY-MM-DD") }
});
Sample Data
Let us create a collection with documents containing arrival dates ?
db.demo91.insertMany([
{ "ArrivalDate": new ISODate("2020-01-10") },
{ "ArrivalDate": new ISODate("2019-12-14") },
{ "ArrivalDate": new ISODate("2020-01-15") }
]);
{
"acknowledged": true,
"insertedIds": [
ObjectId("..."),
ObjectId("..."),
ObjectId("...")
]
}
Display all documents from the collection ?
db.demo91.find();
{ "_id": ObjectId("..."), "ArrivalDate": ISODate("2020-01-10T00:00:00Z") }
{ "_id": ObjectId("..."), "ArrivalDate": ISODate("2019-12-14T00:00:00Z") }
{ "_id": ObjectId("..."), "ArrivalDate": ISODate("2020-01-15T00:00:00Z") }
Example: Find Records On or After January 10, 2020
Query to find all records with arrival date on or after January 10, 2020 ?
db.demo91.find({
"ArrivalDate": { $gte: ISODate("2020-01-10") }
});
{ "_id": ObjectId("..."), "ArrivalDate": ISODate("2020-01-10T00:00:00Z") }
{ "_id": ObjectId("..."), "ArrivalDate": ISODate("2020-01-15T00:00:00Z") }
Key Points
-
$gteincludes the exact date specified in the query condition. - Always use
ISODate()for proper date comparisons in MongoDB. - Dates are stored in UTC format by default.
Conclusion
Use $gte with ISODate() to find records on or after a specific date. This operator performs inclusive comparison, returning documents with dates greater than or equal to the specified value.
Advertisements
