title: Mongo DB aggregate query in php
body: <p>I am new in MongoDB so facing issue in making join query in mongo please help
  I have two collection "user" and "user_login_details"</p>
  
  <p>Previously I have below query to fetch record from single collection</p>
  
  <pre><code>$data = $this-&gt;mongodb-&gt;select()-&gt;where(array('instituteId' =&gt; $instituteId))-&gt;where_not_in('type', array($instituteAdmin))-&gt;get('user');
  </code></pre>
  
  <p>Now I want to fetch records from two collection "user" have single row and for that row multiple entries of login detail present in "user_login_details" collection.</p>
  
  <p>I try with aggregate as below</p>
  
  <pre><code>db.user.aggregate([
  {$match: { instituteId: '5954ee6dcf2169d50d51b88d'} },
  {
   $lookup:
     {
       from: "user_login_details",
       localField: "_id",
       foreignField: "user_id",
       as: "user_login_details"
     }
  }
  ]).pretty()
  </code></pre>
  
  <p>But need one more condition as per my first query in php</p>
  
  <pre><code>-&gt;where_not_in('type', array($instituteAdmin))
  </code></pre>
  
  <p>How I can add this in aggregate query.</p>
  
  <p>Also above query will return data with multiple login details as below.</p>
  
  <pre><code>{
  "_id" : ObjectId("5954ee6dcf2169d50d51b88e"),
  "email" : "abc@gmail.com",
  "instituteId": "5954ee6dcf2169d50d51b88d",
  "type": "Institute Admin"
  "user_name" : "SA-011",
  "religion" : "",
  "caste" : "",
  "bloodGroup" : "",
  "groups" : "",
  "reset_password" : NumberLong(1),
  "aws_arn" : "",
  "user_login_details" : [
      {
          "_id" : ObjectId("5b069955df05aeda0f8b4568"),
          "user_id" : ObjectId("5954ee6dcf2169d50d51b88e"),
          "ip" : "::1",
          "my_session_id" : "3199d6fd6ecfef1ec63bc1760115d116",
          "login_time" : ISODate("2018-05-24T10:52:05Z"),
          "logout_time" : ISODate("2018-05-24T11:04:47Z")
      },
      {
          "_id" : ObjectId("5b069c52df05ae6e358b4567"),
          "user_id" : ObjectId("5954ee6dcf2169d50d51b88e"),
          "ip" : "::1",
          "my_session_id" : "b2d33ea8bc7cc3bb8090fc1dd15f7371",
          "login_time" : ISODate("2018-05-24T11:04:50Z"),
          "logout_time" : ""
      }
  ]
  }
  </code></pre>
  
  <p>I need only latest record from "user_login_details" collection.</p>
  
  <p>Please help by creating query in PHP as I used "CodeIgniter MongoDB Active Record Library".</p>
  
  <p>Thanks in advance.</p>