title: $lookup with php mongodb
body: <p>I have a Student Table in mongo db like </p>
  
  <pre><code>      {
         "_id": ObjectId("5baa85f61d7859401000002a"),
          "Name": "Bella Dave",
           "RollNo": 12,
          "Class": "Ist",
          "TransportDetails": [
           {
              "RouteId": ObjectId("5baa93a21d7859401000002b"),
              "StopId": "abc123",
              "Status": "Inactive" 
           },
           {
              "RouteId": ObjectId("5baa818d1d78594010000029"),
              "StopId": "abc456",
              "Status": "Active" 
           } 
         ] 
       }
  </code></pre>
  
  <p>I have Route Table like</p>
  
  <pre><code>  {
       "Name": "New york City",
       "StopDetails": [
        {
          "StopId": "abc123",
          "Name": "Block no 1"
        },
       {
         "StopId": "abc567",
         "Name": "Block no 2"
       }
     ]        
  </code></pre>
  
  <p>I have written below aggregation query like</p>
  
  <pre><code>       $cursor = $this-&gt;db-&gt;TblStudent-&gt;aggregate([
      [
          '$addFields' =&gt; [
              'ActiveRouteId' =&gt; [
                  '$map' =&gt; [
                      'input' =&gt; '$TransportDetails',
                      'as' =&gt; 'item',
                      'in' =&gt; [
                          '$cond' =&gt; [
                              ['$eq' =&gt; ['$$item.Status', "Active"]],
                              '$$item.RouteId',
                              false
                          ]
                      ]
                  ]
              ]
          ]
      ],
       [
          '$addFields' =&gt; [
              'ActiveStopId' =&gt; [
                  '$map' =&gt; [
                      'input' =&gt; '$TransportDetails',
                      'as' =&gt; 'item',
                      'in' =&gt; [
                          '$cond' =&gt; [
                              ['$eq' =&gt; ['$$item.Status', "Active"]],
                              '$$item.StopId',
                              false
                          ]
                      ]
                  ]
              ]
          ]
      ],
       array(
              '$lookup' =&gt; array(
                  'from' =&gt; 'TblRoute',
                  'localField' =&gt; 'ActiveRouteId',
                  'foreignField' =&gt; '_id',
                  'as' =&gt; 'RouteDetails'
              )
          ),
       array(
              '$lookup' =&gt; array(
                  'from' =&gt; 'TblRoute',
                  'localField' =&gt; 'ActiveStopId',
                  'foreignField' =&gt; 'StopDetails.StopId',
                  'as' =&gt; 'StopDetails'
              )
          ),
       ])-&gt;toArray();
  
      return $cursor;
  </code></pre>
  
  <p>Basically, I have to get active Route and Stop information along with student data. So, I successfully fetched ActiveRouteId and ActiveStopId using $addFields and $map operators. Based on ActiveRouteId, I am doing $lookup for fetching active route information. I am successfully getting that in "RouteDetails" embedded document. Now I issue is in the line</p>
  
  <pre><code>            array(
              '$lookup' =&gt; array(
                  'from' =&gt; 'TblRoute',
                  'localField' =&gt; 'ActiveStopId',
                  'foreignField' =&gt; 'StopDetails.StopId',
                  'as' =&gt; 'StopDetails'
              )
          ),
  </code></pre>
  
  <p>This lookup is not fetching anything. Please help!!!      </p>
  
  <p>Is it possible to find route and stop information together. I mean in route table there are many other embedded documents, if it is possible to fetch the  required embedded document like </p>
  
  <pre><code>      RouteDetails: [      
       "Name": "New york City",
       "StopDetails": [
        {
          "StopId": "abc123",
          "Name": "Block no 1"
        }
       ]
  </code></pre>