Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught MongoDB\Driver\Exception\ConnectionException: $or must be an array - PHP

Tags:

php

mongodb

I am trying to query using an $or operator using MongoDB's PHP driver, but I am being given the following error:

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

Here is my code:

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);
like image 811
ryansin Avatar asked Mar 09 '17 15:03

ryansin


1 Answers

After a bit more research it appears that I was misunderstanding the $or operator, which I interpreted as meaning that you simply have to specify an array of keys/value expressions and if any match, then the result is true.

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

To resolve this, I simply needed to specify an array object for each expression, since an array of multiple expressions evaluates in MongoDB to $and.

So the resolution was:

$filter = ['$or' => [['subdomain' => $postSub], ['email' => $postEmail]]];
like image 111
ryansin Avatar answered Sep 20 '22 08:09

ryansin