[英]php mongo db $in query for multiple keys from DB not working
我有一个问题,当我使用mongoDB
值时查询无法工作,但是当我尝试使用静态/硬编码值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
提前致谢
有两个错误:
每个人中的第一个。 无需添加ID,而是添加'...'
。
foreach ($catlist as $key => $value){
$cattype[] = $value['cid']
}
第二点在您的陈述中。 您想传递$cattype
数组,但是$cattype
已经是一个数组,因此您将其再次包装在数组中。
$cursor = $customer->find([
'provider_id' => "0",
'cat_id' => ['$in' => $catlist]
]);
请先使用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))));
$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))
);
我找到答案了。 无需内爆$ cattype。 只需在$ in内直接给$ cattype像array('$ in'=> array($ cattype))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.