我想创建一个动态的MongoDB查询其插入它的每一部分的聚集,如果条件为真,否则不注射的那部分。如何传递一个空的PHP变量MongoDB中查询
比如我要检查,如果时间是凌晨1点至早上8点。如果是,则定义的数组到MongoDB的查询其他人处理,没有在传递。
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
]
]
]
]);
});
但在这个例子中它会注入到空查询,使得它错了,我赶上错误:bad query: BadValue: $or/$and/$nor entries need to be full objects
我已经改成了$this->N_IN_1_8 = '';
仍然没有成功。
我需要一个中立的变量或条件,其中通过如果要是假的条件不会对查询的影响。 任何想法?
FYI:我使用Laravel 5.3 jenssegers/laravel-mongodb的 包框架与mongodb的工作
感谢您的教育答案 - 但是当我第二个选项去我得到:Aggregate.php线93 : $管道是不是列表(意外指数:“$匹配”) –
谢谢你这样做! – malarzm