格式化mongo查询

时间:2019-04-16 09:22:53

标签: mongodb php-5.6

我有以下代码:

$group = [];
$comparison = '=';
$group = array('$and'=>array());
$filters = ['abc', 'xyz'];

foreach ($filters as $filter) {
  $group['$and'][] = array(
  'team_id' => array (
    ($comparison == '=' ? '$nin' : '$in') => array($filter)
   )
 ); 
}
echo json_encode($group, true);

我尝试了一些诸如$ group ['$ and'] [] ['team_id'] [$ comparison] = $ filter之类的事情;在foreach循环中没有运气

这会产生以下结果:

{  
   "$and":[  
      {  
         "team_id":{  
            "$nin":[  
               "abc"
            ]
         }
      },
      {  
         "team_id":{  
            "$nin":[  
               "xyz"
            ]
         }
      }
   ]
}

我正在尝试获取:

"$and":[  
            {  
               "team_id":{  
                  "$nin":[  
                     "abc",
                     "xyz"
                  ]
               }
            }
         ]

0 个答案:

没有答案