How to pass empty php variable in mongodb query

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

+5
source share
1 answer

Preferring to keep the code structure as it is, that is, when the pre-constructed array is not used in the condition code, you can save $MidnightCheck as null and wrap the array_filter array:

 return $collection->aggregate([ [ '$match' => [ '$and' => array_filter([ ["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 ]) ] // ... 

Calling array_filter without a second argument will filter out all false values ​​from the array, as a result of which the unwanted $MidnightCheck will be deleted.

What, in my opinion, can be clearer is to pre-prepare the conditions in the callback:

 $conditions = [ ["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"]], ]; if ($MidnighCheck) { $conditions[] = $MidnightCheck; } return $collection->aggregate([ ['$match' => [ '$and' => $conditions ],] ]) 
+1
source

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


All Articles