Bin Chen
Bin Chen

Reputation: 63349

mongodb: how to filter out the records that created today?

I have a collection with a timestamp field of type datetime.now():

"chashi_ts" : ISODate("2012-06-12T16:08:22.645Z")

With only mongodb commandline, how to find the records that the field's datetime points to today?

Upvotes: 3

Views: 9212

Answers (1)

Michael DeLorenzo
Michael DeLorenzo

Reputation: 1140

Assuming you can get the current date....

// the start of the day (midnight)
begin_date_range = ISODate("2012-06-12T00:00:00Z");

// the start of the next day (at midnight)
// this can also be done with 11:59:59PM of the current date using $lte
end_date_range = ISODate("2012-06-13T00:00:00Z");

results = db.collection_name.where({"chashi_ts":{
              "$gte":begin_date_range,
              "$lt":end_date_range}
          );

Upvotes: 8

Related Questions