0
votes

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

2
Can anybody please help how to rid of this error?Sandip Nag

2 Answers

1
votes

Use single quote for '$lookup' or escape it "\$lookup", or PHP will treat it as a variable, which is undefined/empty

0
votes

This is working with use of toArray();

error_reporting(1); $collection = $this->mongo->your_db->admin;

    $ops = array(

        array(
            '$lookup' => array(
                "from" => "users",
                "localField" => "id",
                "foreignField" => "user_id",
                "as" => "inventory_docs"
            )
        )

    );
    $results = $collection->aggregate($ops)->toArray();
    echo "<pre>";    print_r($results);exit;