简体   繁体   中英

How can I try to get information from 2 or more collections in MongoDB using PHP driver

I need to get information about 2 different collections in MongoDB using PHP mongo driver.

I know that recently Mongo University added a new feature called 'lookup' but I don't know how can I apply this option in the php mongo find() method. (Seems that is not ready for php)

Anyway what could be the best option to get information from more than a collection?

Create a temporal collection adding the information from each collection?

Loopkup is a Pipeline stage for the Aggregation Framework.

You can use it as a stage in the aggregate method of a collection. Check the php doc about it here .

As example, referring to a sample data of the doc here you can try the following code:

$m = new MongoClient("localhost");
$c = $m->selectDB("examples")->selectCollection("orders");

$ops = array(
    array(
        '$lookup' => array(
            'from' => 'inventory',
            'localField'   => 'item',
            'foreignField' => 'sku',
            'as' => 'inventory_docs'
        )
    );
$results = $c->aggregate($ops);

Hope this help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM