How to find all objects created before specified Date in MongoDB?

To find all objects created before a specified date in MongoDB, use the $lt (less than) operator with ISODate() to compare date values in your query condition.

Syntax

db.collection.find({
    "dateField": { $lt: ISODate("YYYY-MM-DD") }
});

Sample Data

db.beforeSpecifyDateDemo.insertMany([
    { "UserLoginDate": new ISODate('2016-03-21') },
    { "UserLoginDate": new ISODate('2016-05-11') },
    { "UserLoginDate": new ISODate('2017-01-31') },
    { "UserLoginDate": new ISODate('2018-05-15') },
    { "UserLoginDate": new ISODate('2019-04-01') }
]);
{
    "acknowledged": true,
    "insertedIds": [
        ObjectId("5cbd91e4de8cc557214c0e0d"),
        ObjectId("5cbd91ecde8cc557214c0e0e"),
        ObjectId("5cbd91f9de8cc557214c0e0f"),
        ObjectId("5cbd9206de8cc557214c0e10"),
        ObjectId("5cbd9211de8cc557214c0e11")
    ]
}

Display All Documents

db.beforeSpecifyDateDemo.find().pretty();
{
    "_id": ObjectId("5cbd91e4de8cc557214c0e0d"),
    "UserLoginDate": ISODate("2016-03-21T00:00:00Z")
}
{
    "_id": ObjectId("5cbd91ecde8cc557214c0e0e"),
    "UserLoginDate": ISODate("2016-05-11T00:00:00Z")
}
{
    "_id": ObjectId("5cbd91f9de8cc557214c0e0f"),
    "UserLoginDate": ISODate("2017-01-31T00:00:00Z")
}
{
    "_id": ObjectId("5cbd9206de8cc557214c0e10"),
    "UserLoginDate": ISODate("2018-05-15T00:00:00Z")
}
{
    "_id": ObjectId("5cbd9211de8cc557214c0e11"),
    "UserLoginDate": ISODate("2019-04-01T00:00:00Z")
}

Example: Find Objects Before 2017-01-31

db.beforeSpecifyDateDemo.find({
    UserLoginDate: { $lt: ISODate("2017-01-31") }
});
{ "_id": ObjectId("5cbd91e4de8cc557214c0e0d"), "UserLoginDate": ISODate("2016-03-21T00:00:00Z") }
{ "_id": ObjectId("5cbd91ecde8cc557214c0e0e"), "UserLoginDate": ISODate("2016-05-11T00:00:00Z") }

Key Points

  • $lt operator finds documents where the date field value is strictly less than the specified date
  • Use ISODate() for proper date comparison in MongoDB
  • The comparison excludes the exact date specified in the query condition

Conclusion

The $lt operator with ISODate() efficiently queries MongoDB documents created before a specific date. This approach ensures accurate date comparisons and returns all matching documents that precede your target date.

Updated on: 2026-03-15T00:51:12+05:30

633 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements