Find posts that are older than current date in MongoDB?

To find posts older than current date in MongoDB, use the $lte operator with new Date() to compare document dates against the current system date.

Syntax

db.collection.find({
    dateField: { $lte: new Date() }
});

Sample Data

Let us create a collection with documents containing different due dates ?

db.demo746.insertMany([
    { DueDate: new Date("2020-01-10") },
    { DueDate: new Date("2020-10-10") },
    { DueDate: new Date("2020-03-05") },
    { DueDate: new Date("2020-05-04") }
]);
{
    "acknowledged": true,
    "insertedIds": [
        ObjectId("5eae67eca930c785c834e55b"),
        ObjectId("5eae67eda930c785c834e55c"),
        ObjectId("5eae67eea930c785c834e55d"),
        ObjectId("5eae67f1a930c785c834e55e")
    ]
}

Display Sample Data

db.demo746.find();
{ "_id": ObjectId("5eae67eca930c785c834e55b"), "DueDate": ISODate("2020-01-10T00:00:00Z") }
{ "_id": ObjectId("5eae67eda930c785c834e55c"), "DueDate": ISODate("2020-10-10T00:00:00Z") }
{ "_id": ObjectId("5eae67eea930c785c834e55d"), "DueDate": ISODate("2020-03-05T00:00:00Z") }
{ "_id": ObjectId("5eae67f1a930c785c834e55e"), "DueDate": ISODate("2020-05-04T00:00:00Z") }

Find Posts Older Than Current Date

Query to find posts with due dates older than today ?

db.demo746.find({
    DueDate: { $lte: new Date() }
});
{ "_id": ObjectId("5eae67eca930c785c834e55b"), "DueDate": ISODate("2020-01-10T00:00:00Z") }
{ "_id": ObjectId("5eae67eea930c785c834e55d"), "DueDate": ISODate("2020-03-05T00:00:00Z") }
{ "_id": ObjectId("5eae67f1a930c785c834e55e"), "DueDate": ISODate("2020-05-04T00:00:00Z") }

Key Points

  • $lte means "less than or equal to" − includes dates equal to current date
  • new Date() gets the current system date and time
  • Use $lt if you want strictly older dates (excluding today)

Conclusion

The $lte operator with new Date() effectively filters documents containing dates older than or equal to the current date. This approach is commonly used for finding overdue items or expired content.

Updated on: 2026-03-15T03:55:13+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements