我有一个问题,当我使用mongoDB
中的值时查询无效,但在我尝试静态/硬编码值时有效。
你能指出我做得不好吗?
当我对下面的值进行硬编码时,代码运行正常。
$customer = $this->mongo->selectCollection('bookings');
$cursor = $customer->find(array('provider_id' => "0", 'cat_id' => array('$in' => array('591d380c227951b706e18a56','591da979227951a10f004ad7','591d42292279517209004ad7'))));
但是当值来自mongoDB时,此查询无效。
$cattype = array();
foreach ($catlist as $key => $value){ $cattype[] = "'".$value['cid']."'";}
$catlist = implode(',', $cattype);
$customer = $this->mongo->selectCollection('bookings');
$cursor = $customer->find(array('provider_id' => "0", 'cat_id' => array('$in' => array($catlist))));
$catlist = comes from mongoDB
提前致谢
答案 0 :(得分:0)
有两个错误:
第一个是你的每一个。您可以添加'...'
。
foreach ($catlist as $key => $value){
$cattype[] = $value['cid']
}
第二个是在你的陈述中。您想要传递$cattype
数组,但$cattype
已经是一个数组,因此您将其再次包装在数组中。
$cursor = $customer->find([
'provider_id' => "0",
'cat_id' => ['$in' => $catlist]
]);
答案 1 :(得分:0)
请首先使用mongo id转换, 它可能对你有帮助。
$cattype = array();
foreach ($catlist as $key => $value) {
$cattype[] = new \MongoId($value['cid']);
}
$catlist = implode(',', $cattype);
$customer = $this->mongo->selectCollection('bookings');
$cursor = $customer->find(array('provider_id' => "0", 'cat_id' =>
array('$in' => array($catlist))));
答案 2 :(得分:0)
$cattype = array();
foreach ($catlist as $key => $value) {
$cattype[] = $value['cid'];
}
$catlist = implode(',', $cattype);
$customer = $this->mongo->selectCollection('bookings');
$cursor = $customer->find(array('provider_id' => "0", 'cat_id' =>
array('$in' => $catlist))
);
答案 3 :(得分:0)
我找到了答案。无需破坏$ cattype。只需将$ cattype直接放在$ in array中('$ in'=>数组($ cattype))