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);
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]]];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With