0

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

1
  • Can anybody please help how to rid of this error? Commented Feb 7, 2018 at 9:30

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

,you are correct.I was getting same issue but after escape character its working fine. thanks
0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.