There is a collection with documents, in each document there is an array field, a document I find across the field 'key', then in each found document I select an array field 'obyav', with the aim then to combine these arrays. I do it like this in a primitive way:

$dok1 = $collection->findOne(array('key' => $key1)); $dok2 = $collection->findOne(array('key' => $key2)); $dok3 = $collection->findOne(array('key' => $key3)); $mas1 = $dok1['obyav']; $mas2 = $dok2['obyav']; $mas3 = $dok3['obyav']; 

It turns out three requests to Mongo, you can do it like that for one request or more beautifully. So that at the exit I had a combined array of $ mas1 $ mas2 $ mas3?

    1 answer 1

    Decision

    Yes, it is possible to select data in one query, use .find (not .findOne ) and $ in operator. By crossing the documentation and your field names, we get:

     {key: {$in: [key1, key2, ... keyN ]}} 

    The result will be - an array of documents with the listed key values

    When translated into PHP, you get something fun, like:

     array('key'=>array('$in'=>array(key1, key2, ... keyN))) 

    PS:

    1. The result - turn the array of documents into an array of fields with the help of array_map .

    2. Use the projection argument when searching for documents. This will allow you to select only the required fields from the database.