简体   繁体   中英

Why doesn't this PHP MongoDB query return any results even though there is one?

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?

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'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM