title: Mongo PHP join data from another collection and update doc - like SQL JOIN
body: <p>I need help with MongoDb with PHP driver.</p>
  
  <p>I have 4 collections:</p>
  
  <ol>
  <li><p>order_aproved : </p>
  
  <pre><code>{
     "order_id" : mongoId ,
     "user_id":num ,
     "order_date":mongoDate ,
     "requset" : string 
  }
  </code></pre></li>
  <li><p>orders_rejected :</p>
  
  <pre><code>{
    "order_id" : mongoId,
    "user_id" : num ,
    "order_date" : mongoDate ,
    "requset" : string
  }
  </code></pre></li>
  <li><p>users :  </p>
  
  <pre><code>{
    "user_id" : mongoId,
    "username" : num ,
    "last_order" : mongoDate ,
    "num_orders" : num,
    "last_order"
  }
  </code></pre></li>
  <li><p>orders_log : </p>
  
  <pre><code>{
     "order_id" : mongoId  ,
     "order_date" : mongoDate ,
     "status" : boolen ,
     "user_id" : num
  }
  </code></pre></li>
  </ol>
  
  <p>Every approved/rejected order, I update the <code>num_orders</code> on user document
  that have a new/rejected order. So that number is always changing
  and log that order on <code>orders_log</code>.</p>
  
  <p>I need to fetch all orders approved/rejected on <code>orders_log</code> by list of users <code>[array]</code> with condition and get the orders count <code>num_orders</code> and last order date for that user by the order from this user</p>
  
  <p>I am doing it like this:</p>
  
  <pre><code>$cursor = $orders-&gt;find()-&gt;sort(array("order_date" =&gt; -1))-&gt;limit(15);
  $array = iterator_to_array($cursor,false);
  
  $users_for_aproved = ["123","124","125"];
  $users_for_rejcted = ["112","113","114"];
  
  $js = "function() { if ( this.requset )  { return this.requset.length &gt; 0 } }"; 
  
  $query1 = array( '$and' =&gt; array(
      array("user_id" =&gt; array('$in'=&gt; $users_for_aproved)),
      array('$where' =&gt; $js )
  ));
  
  $query1 = array( '$and' =&gt; array(
      array("user_id" =&gt; array('$in'=&gt; $users_for_rejcted)),
      array('$where' =&gt; $js )
  ));
  
  $query_or = array('$or' =&gt; array($query,$query1);
  
  $cursor = $orders_log-&gt;find($query_or)-&gt;sort(array("order_date" =&gt; -1))-&gt;limit(15);
  $array = iterator_to_array($cursor,false);
  
  for (  $x=0; $x &lt; count($array) ; $x++ ) {
  
      $query = array( "user_id" =&gt; $order["user_id"] );
      $cursor = $orders-&gt;find($query)-&gt;limit(1);
      $array = iterator_to_array($cursor,false);
  
      $order_count = $array[0]["num_orders"];
      $array[$x]["order_count"] = $order_count;
  }
  
  return $array;
  </code></pre>
  
  <p>It's working but its not very efficient , i need a way to fetch data from another collection and add the num_orders to the doc that i have find without a use form anther query</p>
  
  <p>like SQL JOIN but on mongo and php driver
  Thanks!</p>