Query with MongoDate vs UTCDateTime

After upgrading to the new Mongo driver for PHP, I ran into a sorting and query problem.

The old driver used: http://php.net/manual/en/class.mongodate.php , which stores the dates in the MongoDate object in seconds.

New driver: http://php.net/manual/en/class.mongodb-bson-utcdatetime.php saves the date in a different format and saves it in milliseconds.

A request has occurred using $ gte or $ lte useless. Example:

$collection -> find(array('start_date' => array('$gte' => new MongoDate()))); $collection -> find(array('start_date' => array('$gte' => new MongoDB\BSON\UTCDateTime()))); 

These two do not return the same result. With all the old data, how can I still safely request both MongoDate and UTCDateTime?

+6
source share
1 answer

You need to pass the time in milliseconds instead of seconds, and everything will work as before:

 $time = time(); $cursor = $collection->find(['start_date' => ['$gte' => new MongoDB\BSON\UTCDateTime($time * 1000)]]); 
0
source

All Articles