MongoDB find today's records

mongodb

Solution

we can use `$where`

db.collection.find(
   { $where: "this._id.getTimestamp() >= ISODate('2017-02-25')" }
)

To get documents for today, or better say from last midnight:

db.collection.find( { $where: function() { 
    today = new Date(); //
    today.setHours(0,0,0,0);
    return (this._id.getTimestamp() >= today)
} } );

of course it is much faster to have an indexed timestamp field or to follow the approach with the calculation of an ObjectID for the start date and compare _id against it, as _id is indexed too.

Problem

In Mongo shell, how would I filter records that have been added today (or on a specific date)? I have no specific field of the timestamp of new records, but I guess it can be restored from ObjectID.

Original source

Related problems