I want to create a dynamic mongodb query that inserts every part of its aggregation if the condition is true, otherwise do not enter this part.
For example, I want to check if the time is between 1AM and 8AM. If so, then pass the mongodb request in a specific array, otherwise do not skip anything.
if ($this->Not_in_1_8 == true) { $this->N_IN_1_8 = array('dont_show_between_1_n_8' => array('$ne' => true)); }else { $this->N_IN_1_8 = null; } $MidnightCheck = $this->N_IN_1_8; $this->campaign = Bcamp::raw(function ($collection) use ($seat_category_list, $banner_size, $seat_filter_list, $gold_network, $MidnightCheck) { return $collection->aggregate([ [ '$match' => [ '$and' => [ ["targets.cats" => [ '$in' => $seat_category_list ] ], ['banners.' . $banner_size => [ '$exists' => true ] ], ['href' => [ '$nin' => $seat_filter_list ] ], ['targets.gold_network' => [ '$eq' => $gold_network ] ], ['status' => [ '$ne' => "Low_Budget" ] ], ['daily_budget_status' => [ '$ne' => "Low_Budget" ] ], $MidnightCheck ] ] ], [ '$project' => [ 'ab' => [ '$cmp' => [ '$budget', '$click_cost' ] ] ] ], [ '$match' => [ 'ab' => [ '$lt' => 1 ] ] ] ]); });
But in this example, it will enter null into the query and makes it wrong, and I will catch the error: bad query: BadValue: $or/$and/$nor entries need to be full objects
I changed it to $this->N_IN_1_8 = ''; until you get success.
I need a neutral variable or condition for the transfer that does not affect the request if the if condition is false. Any ideas?
FYI: I am using the Laravel 5.3 framework with jenssegers / laravel-mongodb package to work with mongodb
source share