title: Aggregate $filter Between Elements Matching Conditions
body: <hr>
  
  <p>Data:</p>
  
  <pre><code>[
  
  {
      "_id" : ObjectId("597c48d222b29fc421e82d20"),
      "Date" : "12/06/2017",
      "Country" : "DEMO",
      "RiderId" : "DEMO",
      "VehicleId" : "DEMO",
      "StartAddress" : "Colombo",
      "StartLocation" : [ 
          6.9270974, 
          79.8612478
      ],
      "EndAddress" : "Kegalle,",
      "EndLocation" : [ 
          7.2476005, 
          80.3483415
      ],
      "DepartureAddress" : "Dellogistics International (Pvt) Ltd, Colombo 04",
      "DepartureLocation" : [ 
          6.8824893, 
          79.8620031
      ],
      "ArrivalAddress" : "Osro, Kegalle",
      "ArrivalLocation" : [ 
          7.2476005, 
          80.3483415
      ],
      "IsLadiesOnly" : false,
      "Notes" : "\"I'm travalleing from Bambalapitiya to Kegalle, small luggages are ok\"",
      "Luggage" : 2.0,
      "Detours" : 1.0,
      "Route" : {
          "Bounds" : {
              "NorthEast" : [ 
                  7.2916216, 
                  80.6341326
              ],
              "SouthWest" : [ 
                  6.9270974, 
                  79.8607731
              ]
          },
          "Legs" : [ 
              {
                  "LegId" : 0.0,
                  "Distance" : 40033.0,
                  "Duration" : 4725.0,
                  "Price" : "240",
                  "StartAddress" : "Colombo",
                  "StartLocation" : [ 
                      6.9270974, 
                      6.9270974
                  ],
                  "EndAddress" : "Nittambuwa",
                  "EndLocation" : [ 
                      7.1420863, 
                      80.1038061
                  ],
                  "Ancestors" : []
              }, 
              {
                  "LegId" : 1.0,
                  "Distance" : 18008.0,
                  "Duration" : 1850.0,
                  "Price" : "480",
                  "StartAddress" : "Nittambuwa",
                  "StartLocation" : [ 
                      7.1420863, 
                      80.1038061
                  ],
                  "EndAddress" : "Warakapola",
                  "EndLocation" : [ 
                      7.2268383, 
                      80.1959644
                  ],
                  "Ancestors" : [ 
                      "Colombo"
                  ]
              }, 
              {
                  "LegId" : 2.0,
                  "Distance" : 22478.0,
                  "Duration" : 2208.0,
                  "Price" : "720",
                  "StartAddress" : "Warakapola",
                  "StartLocation" : [ 
                      7.2268383, 
                      80.1959644
                  ],
                  "EndAddress" : "Kegalle",
                  "EndLocation" : [ 
                      7.2514362, 
                      80.3466076
                  ],
                  "Ancestors" : [ 
                      "Colombo", 
                      "Nittambuwa"
                  ]
              }
          ]
      },
      "CreatedDate" : "2017-01-06T07:00:00.000Z",
      "ModifiedDate" : "01/06/2017 12:30",
      "Points" : "123"
  }]
  </code></pre>
  
  <p>And the query is :</p>
  
  <pre><code> db.trips.aggregate([
         {$match: { "Route.Legs": { $elemMatch: { "StartAddress": "Nittambuwa","Ancestors":{$nin:["Kegalle"]}} }, "Route.Legs.EndAddress":"Kegalle" }},
          {  $project: {
               RiderId: "$RiderId",
               Legs: {
                  $filter: {
                     input: "$Route.Legs",
                     as: "leg",
                     cond: {
                            "$and": [
                              { "$gte": [ "$$leg.LegId", &lt;**Get the leg id by passing the  start address**&gt; ] },
                              { "$lte": [ "$$leg.LegId", 3 ] }
                            ]
                          }
                  }
               }
            }}
  
      ])
  </code></pre>
  
  <p>Basicaly inside the condition i need to query the collection and get the leg ID for comparison. i tried $where but it doesn't recognize the operator inside the condition.</p>
  
  <p>Also why i cannot use $where, $nin inside the cond</p>