MongoDB Raw request for selecting documents in a given time period

I want to select documents that are specified on a given date of the date, for example between

$from_date : "2017-01-07 09:08:59" To `$to_date : "2017-08-09 09:08:59"` 

I am using the laravel framework and the janssegers / laravel-mongodb mongodb driver to run my request. And this is my original request:

 $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();

But my problem is that it returns 0 as a result; while during this period there are 16 documents.

I tried this method to get their number, but still 0 result:

   $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();

FYI: I converted the input date and time to ISODate. What is the mongodb data type for the field created_at.

$Fromdatetime = $request->input('from_date');
$from_date = new DateTime($Fromdatetime);
$from_date = $from_date->format(DateTime::ISO8601);

Mongodb field data type:

 "updated_at": ISODate("2017-04-10T09:35:58.641Z"),
   "created_at": ISODate("2017-04-10T09:35:58.641Z")

My input type is: "2017-01-07T09:08:59+0000"

Any suggestion?

+4
2

, 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();

jessenger, whereBetween , where, .

.

+4

" " " ", $to_date updated_at, Eloquent, created_at

$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();
+4

Source: https://habr.com/ru/post/1674622/


All Articles