Home:ALL Converter>Mongodb $lookup query using PHP driver not working

Mongodb $lookup query using PHP driver not working

Ask Time:2018-02-07T17:10:27         Author:Sandip Nag

Json Formatter

I want to get data from two collections where there is a common field between this two collections.I am giving both queries for Mongo and PHP. My PHP version is PHP 5.6.31-1~ubuntu14.04.1+deb.sury.org+1 (cli) My mongo db query for between two collections:

db.dental_refset.aggregate(
[
    {   $lookup:
        {
            from:"v20170731",
            localField:"referencedComponentId",
            foreignField:"conceptId",
            as:"joinedData"
        }
    }

])

I am ding this in php and my code is below:

error_reporting(1); 
require 'driver/vendor/autoload.php'; 
$client = new MongoDB\Client("mongodb://192.168.2.95:27017");
$collection = $client->selectDatabase('en-edition')->selectCollection("dental_refset");
$ops = array(
    array(
        "$lookup" => array(
            "from" => "v20170731",
            "localField" => "referencedComponentId",
            "foreignField" => "conceptId",
            "as" => "user_docs"
        )
    )
);
$results = $collection->aggregate($ops);
print_r($collection);

But error occurs. Error has given below:

Uncaught MongoDB\Driver\Exception\RuntimeException: Invalid filter: empty key in /var/www/html/mednxtMod/mednxt/driver/vendor/mongodb/mongodb/src/Operation/Aggregate.php:223\nStack trace:\n#0 /var/www/html/mednxtMod/mednxt/driver/vendor/mongodb/mongodb/src/Operation/Aggregate.php(223): MongoDB\Driver\Server->executeCommand('en-edition', Object(MongoDB\Driver\Command), Object(MongoDB\Driver\ReadPreference))\n#1 /var/www/html/mednxtMod/mednxt/driver/vendor/mongodb/mongodb/src/Collection.php(215): MongoDB\Operation\Aggregate->execute(Object(MongoDB\Driver\Server))\n#2 /var/www/html/mednxtMod/mednxt/error.php(19): MongoDB\Collection->aggregate(Array)\n#3 {main}\n thrown in /var/www/html/mednxtMod/mednxt/driver/vendor/mongodb/mongodb/src/Operation/Aggregate.php on line 223Uncaught

Author:Sandip Nag,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/48659809/mongodb-lookup-query-using-php-driver-not-working
anhlc :

Use single quote for '$lookup' or escape it \"\\$lookup\", or PHP will treat it as a variable, which is undefined/empty",
2018-02-08T02:39:29
yy