1

I have a collection of users and users have a meta.create_date field which is an ISODate as seen below. I am trying to count how many users were created in the last N days. I have the following in the database:

{
    "_id" : ObjectId("51e61fa16803fa40130a0581"),
    "meta" : {
        "create_date" : ISODate("2013-07-17T04:37:53.355Z")
    }
}

My PHP code:

$daysAgo = new MongoDate(date('c', strtotime('-7 days')));

$query = array(
    'meta.create_date' => array(
        '$gte' => $daysAgo,

    )
);

$result = $this->db->users->count($query);

I have also tried specifying a range using '$gte' and '$lte' where $lte => today.

However, result is coming back as 0. So what is going on here?

1 Answer 1

3

MongoDate() takes int time(). So, passing in a php date() to the constructor does not work. This is the proper way:

$daysAgo = new MongoDate(strtotime('-7 days'));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.