I want to get data from mongodb by using MongoDB PHP7.1 Driver with the help of $or or $and clause in filter. I have tried to construct query to do the same but that didn't work.
Here is my sample code here:
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
try {
$filter = [
'$or' => [
'age' => [ '$gt' => 40],
'name' => 'abc'
]
];
$query = new MongoDB\Driver\Query($filter);
$rows = $manager->executeQuery("test.users", $query);
foreach ($rows as $key => $val) {
print_r($val);
}
} catch(MongoDB\Driver\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
The above code gives me below error:
Fatal error: Uncaught MongoDB\Driver\Exception\ConnectionException: $or must
be an array in C:\xampp\htdocs\local\demo.php:50 Stack trace: #0
C:\xampp\htdocs\local\demo.php(50): MongoDB\Driver\Manager-
>executeQuery('test.users', Object(MongoDB\Driver\Query)) #1 {main} thrown
in C:\xampp\htdocs\local\demo.php on line 50
Please help me to understand what I am doing wrong.
$filter = [ 'name' => 'abc', '$or' => [[ 'age' => [ '$gt' => 40] ], second expression goes here] ];$andthe same.