未捕获的MongoDB \ Driver \ Exception \ ConnectionException:$或必须是一个数组 - PHP

时间:2017-03-09 15:39:41

标签: php mongodb

我正在尝试使用MongoDB's PHP driver使用$or运算符进行查询,但我收到以下错误:

Fatal error: Uncaught MongoDB\Driver\Exception\ConnectionException: $or 
must be an array in /path/to/file.php:83 Stack trace: #0 /path/to/file.php(83):
MongoDB\Driver\Manager->executeQuery('userAccou...',
Object(MongoDB\Driver\Query)) #1 {main} thrown in /path/to/file.php on line 83

这是我的代码:

postSub = $_POST['register']['subdomain'];
$postEmail = $_POST['register']['email'];
$filter = ['$or' => ['subdomain' => $postSub, 'email' => $postEmail]];

$query = new MongoDB\Driver\Query($filter);
$cursor = $mongo->executeQuery('DB_Name.Collection_Name', $query);

1 个答案:

答案 0 :(得分:7)

经过一番研究后,我似乎误解了$or运算符,我将其解释为只需要指定一个键/值表达式数组,如果匹配,则结果为{ {1}}。

true

要解决这个问题,我只需要为每个表达式指定一个数组对象,因为多个表达式的数组在MongoDB中计算为$filter = ['$or' => ['subdomain' => $postSub, 'email' => $postEmail]];

所以决议是:

$and