gpt4 book ai didi

php - 使用 MongoDate 与 UTCDateTime 进行查询

转载 作者:IT老高 更新时间:2023-10-28 13:30:00 25 4
gpt4 key购买 nike

升级到新的 Mongo Driver for PHP 后,我面临排序和查询日期的问题。

使用的旧驱动程序:http://php.net/manual/en/class.mongodate.php以秒为单位将日期存储在 MongoDate 对象中。

新驱动程序:http://php.net/manual/en/class.mongodb-bson-utcdatetime.php以不同的格式存储日期并以毫秒为单位。

这使得使用 $gte 或 $lte 的查询变得毫无用处。示例:

$collection -> find(array('start_date' => array('$gte' => new MongoDate())));

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

这两个不返回相同的结果。有了所有旧数据,我怎样才能安全地同时使用 MongoDate 和 UTCDateTime 进行查询?

最佳答案

您需要以毫秒而不是秒为单位传递时间,一切都会像以前一样继续工作:

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

关于php - 使用 MongoDate 与 UTCDateTime 进行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35776643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com