title: MongoDB - $geoNear with multiple matches in same document
body: <p>I have a collection that looks like this:</p>
  
  <pre><code>{
      "_id" : ObjectId("5783051796a90cd8098fc3e0"),
      "name" : "Org 1",
      "locations" : [ 
          {
              "name" : "loc 1",
              "_id" : ObjectId("57831ac2febcceaf173e81ab"),
              "address" : {
                  "coordinates" : [ 
                      172.6034932, 
                      -43.5333797
                  ]
              }
          }, 
          {
              "name" : "loc 2",
              "_id" : ObjectId("5783436dc57b8d6248c9c196"),
              "address" : {
                  "coordinates" : [ 
                      172.6034977, 
                      -43.5335158
                  ]
              }
          }, 
          {
              "name" : "loc 3",
              "_id" : ObjectId("5783439bc57b8d6248c9c197"),
              "address" : {
                  "coordinates" : [ 
                      168.6626435, 
                      -45.0311622
                  ]
              }
          }
      ]
  }
  </code></pre>
  
  <p>There is a <code>2dsphere</code> index on <code>locations.address.coordinates</code></p>
  
  <p>I'm using <code>aggreagate</code> with <code>geonear</code> to query those documents:</p>
  
  <pre><code>db.organisations.aggregate([
      {
          $geoNear: {
              near: { type: "Point", coordinates: [ 172.6036799 , -43.5335639 ] },
              distanceField: "dist.calculated",
              maxDistance: 5,
              includeLocs: "dist.location",
              num: 5,
              limit: 200,
              spherical: true
          }
      }
  ])
  </code></pre>
  
  <p>This aggregation works fine and I get all the documents with at least 1 location in the selected area.
  The problem is that I would like to retrieve only the relevant locations when there is more than 1 match inside 1 document.</p>
  
  <p>MongoDB is able to tell me which one is the closest with <code>includeLocs</code> option, but that's not enough as I would like all the details.</p>
  
  <p>At least, if I had the distance for each location, I could filter it later on the aggregation pipe. With the <code>distanceField</code> option I can get the distance found for the whole document, but not for each matching location</p>
  
  <p>To recap: <strong>My goal is to find all documents with at least 1 matching location, and know which are the relevant locations among the locations array.</strong></p>
  
  <p>Is there way to get it or am I asking too much to Mongo?</p>