MongoDB PHP using $in with array

Deadly 提交于 2019-12-04 20:59:24

问题


I'm using MongoDB and PHP and trying to do a $in based on a generated array.

When I specify the same array manually, it works, but when I build it, it return any results with the same data.

There's what I have:

$settings = array();
foreach($items as $item) {
   $settings[] = $item['id'];
}

//Settings is the same as this
$setting2 = array(1,2,3,4,5,6,7,8);

//This returns no results
$cursor = $collection->find(array('status' => 0, 'sid' => array('$in' => $settings)));


//This does return results
$cursor = $collection->find(array('status' => 0, 'sid' => array('$in' => $setting2)));

I've checked using

$cursor->info()

And the items in the array are the same.

Any ideas what I'm doing wrong?

Thanks!


回答1:


It's likely that the data types of the numbers are not the same. Try using var_dump() on the built array, and the specified array. You'll probably see one has them as numbers in a string, and the other as simple integers.



来源:https://stackoverflow.com/questions/9950525/mongodb-php-using-in-with-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!