简体   繁体   English

使用PHP库查询mongo日期时遇到问题

[英]Trouble querying mongo dates with PHP library

I'm trying to get all documents that match a certain date range. 我正在尝试获取与特定日期范围匹配的所有文档。 I've created the query in the Mongo CLI successfully, but I'm having trouble translating it to the PHP Mongo library (I just upgraded to PHP 7, so working with the new driver/library is taking some getting used to). 我已经在Mongo CLI中成功创建了查询,但是在将其转换为PHP Mongo库时遇到了麻烦(我刚刚升级到PHP 7,因此使用新的驱动程序/库需要一些时间来适应)。

Why do I get back results in the CLI, but nothing in my code? 为什么我要在CLI中返回结果,但是在我的代码中什么都没有?

$search = [];
if (isset($_GET['days'])) {
    $days = explode(',', $_GET['days']);
    foreach ($days as $day) {
        $date = $dates[$day];
        $start = new DateTime($date, new DateTimeZone('America/New_York'));
        $start = new MongoDB\BSON\UTCDateTime($start);
        $end = new DateTime($date, new DateTimeZone('America/New_York'));
        $end->add(DateInterval::createFromDateString('1 day'));
        $end = new MongoDB\BSON\UTCDateTime($end);
        $search[] = [
            'date.start' => [
                '$gte' => $start,
                '$lte' => $end
            ]
        ];
    }
}
$rEvents = $mongo->events->find($search);

$dates translates the word day to the date it corresponds to. $dates将单词day转换为它对应的日期。 When I check the DateTime objects created, they're correct, but still, no results come back. 当我检查创建的DateTime对象时,它们是正确的,但是仍然没有结果返回。 Where have I gone wrong? 我哪里出问题了?

I worked a bit untill I found out how to do it(it works with php 7 new mongo drivers). 我工作了一段时间,直到发现了如何做(它与php 7新的mongo驱动程序兼容)。

my db="TEST"
my collection="colUser"

So let's start. 因此,让我们开始吧。

1) First we have to instatiate the driver php as following : 1)首先,我们必须实例化驱动程序php,如下所示:

The CODE 编码

 $database=new MongoDB\Driver\Manager;
   // Instantiate the php 7 mongo library
 //2)Prepare the date (mongo uses miliseconds from 1970 not seconds 
//like php)
$date1="2015-01-02";
$date1=new DateTime($date1);
$date1=new MongoDB\BSON\UTCDatetime($date1->getTimeStamp() * 
1000);//Transforming into miliseconds

$date2="2016-06-01";
$date2=new DateTime($date2);
$date2=new MongoDB\BSON\UTCDatetime($date2->getTimeStamp() * 1000);// 
//Transforming second date into ms
//Prepare the array to select time  from $date1 to $date2 from mongo db
$array=array("date"=>array(
                     "\$gte"=>$date1,
                     "\$lt"=>$date2 ));                      
$query=new MongoDB\Driver\Query($array);// Preparing the query
$rows=$database->executeQuery("TEST.colUser",$query);// Getting the data into 
//object $rows
// NOW WHE HAVE TO PRINT THE DATE`s THAT WE FOUND IN THE INTERVAL FROM THE 
//COLLECTIONS
foreach($rows as $k=>$v){// each object $v represents objects from one row
  foreach($v as $key=>$value){// each object from one row
                 if($key=="date"){// "date" is the index time from collection
                     foreach($value as $t=>$time){// $value is an object and 
//it needs an foreach "[milliseconds]->time"
                             $time=$time / 1000; //transform from ms into 
//seconds
                     }
                           echo date("Y-m-d",(int)$time);//finally we are 
//echoing the time in YEAR-MONTH-DAY     
                           echo "<br>";                
                   }
         }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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