Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB原始查询,用于选择给定日期时间段内的文档_Mongodb_Laravel_Mongodb Query_Laravel 5.3_Php 5.6 - Fatal编程技术网

MongoDB原始查询,用于选择给定日期时间段内的文档

MongoDB原始查询,用于选择给定日期时间段内的文档,mongodb,laravel,mongodb-query,laravel-5.3,php-5.6,Mongodb,Laravel,Mongodb Query,Laravel 5.3,Php 5.6,我想选择用户给定日期时间段内的文档,如: $from_date : "2017-01-07 09:08:59" To `$to_date : "2017-08-09 09:08:59"` 我正在使用laravel framework和jenssegers/laravel mongodb mongodb驱动程序运行我的查询,这是我的原始查询: $normal_banners = BannerView::raw(function ($collection) use ($id, $from_da

我想选择用户给定日期时间段内的文档,如:

$from_date : "2017-01-07 09:08:59" To `$to_date : "2017-08-09 09:08:59"` 
我正在使用laravel framework和jenssegers/laravel mongodb mongodb驱动程序运行我的查询,这是我的原始查询:

 $normal_banners = BannerView::raw(function ($collection) use ($id, $from_date, $to_date) {
      $conditions = [
         ["camp_id" => ['$eq' => $id]],
         ['$or' => [
              ['seat_target_status' => ['$eq' => true]],
              ['seat_target_status' => ['$eq' => false]]
         ]],
         ['camp_target_status' => ['$eq' => false]],
         ];

         if ($from_date) {
            $conditions[] = ['created_at' => ['$gte' => $from_date]];
         }

         return $collection->aggregate([
            ['$match' => ['$and' => $conditions]],
         ]);
    })->count();
但我的问题是,结果返回0;而这段时间有16份文件

我尝试了此方法以获取它们的计数,但结果仍然为0:

   $normal_banners = BannerView::Where('camp_id', $id)
        ->where(function ($query) {$query->where('seat_target_status', true)->orWhere('seat_target_status', false);
           })
        ->where('camp_target_status', false)
        ->where("created_at", ">=", $from_date)
        ->count();
仅供参考:我已将输入的日期时间转换为ISODate,这是在字段创建的
的mongodb数据类型

$Fromdatetime = $request->input('from_date');
$from_date = new DateTime($Fromdatetime);
$from_date = $from_date->format(DateTime::ISO8601);
mongodb字段数据类型为:

 "updated_at": ISODate("2017-04-10T09:35:58.641Z"),
   "created_at": ISODate("2017-04-10T09:35:58.641Z")
我的输入数据类型是:
“2017-01-07T09:08:59+0000”

有什么建议吗?

您必须“放在大括号中”或where函数中,在原始查询中,您正在使用更新的列测试$to_date,但在雄辩的代码中,您正在使用创建的列测试$to_date

$targeted_banners = BannerView::Where('camp_id', $id)
  ->where(function($query){$query->where('seat_target_status', true)->orWhere('seat_target_status', false);})
  ->where('camp_target_status', false)
  ->where("created_at", ">=" $from_date)
  ->where("updated_at", "<=", $to_date)
  ->count();
$targeted\u banners=BannerView::Where('camp\u id',$id)
->其中(函数($query){$query->where('seat\u target\u status',true)->或where('seat\u target\u status',false);})
->其中('camp\u target\u status',false)
->其中(“创建时间”、“>=”$from\u日期)

->其中(“updated_at”,”我已经使用Carbon::createDateFrom方法解决了这个问题,它根据我的输入创建了UTC日期时间:

投入:

{
  "from_date": "2017-03-10",
  "to_date": "2017-04-09"
}
转换输入日期:

$from_date_arr = explode("-",$request->input('from_date'));
$from_date = Carbon::createFromDate($from_date_arr[0],$from_date_arr[1],$from_date_arr[2]);

$to_date_arr = explode("-",$request->input('to_date'));
$to_date = Carbon::createFromDate($to_date_arr[0],$to_date_arr[1],$to_date_arr[2]);
这是我运行的有效查询:

$normal_banners = BannerView::Where('camp_id', $id)
    ->where(function ($query) {$query->where('seat_target_status', true)->orWhere('seat_target_status', false);
         })
    ->where('camp_target_status', false)
    ->where("created_at", ">=",$from_date)
    ->where("created_at", "<=",$to_date)
    ->count();
$normal\u banners=BannerView::Where('camp\u id',$id)
->其中(函数($query){$query->where('seat\u target\u status',true)->或where('seat\u target\u status',false);
})
->其中('camp\u target\u status',false)
->其中(“创建时间”、“>=”、$from\u日期)

->其中(“创建于”“创建时间”和“更新时间”之间没有区别,但您是对的,我应该检查一个字段的日期时间检查,让我试试您的方法。@Autisa_z我已经尝试了您的方法-or语句部分正在工作,谢谢,但日期检查仍然不工作,我得到了0结果。@Mohammad_Hosseini和列的格式是什么创建时间/上传时间?它们是ISODate:“更新时间”:ISODate(“2017-04-10T09:35:58.641Z”),“创建时间”:ISODate(“2017-04-10T09:35:58.641Z”)。那么,我想,这就是问题所在,您正在尝试将日期时间格式与ISODate格式进行比较。