title: Mongodb geoNear and group aggregation
body: <p>I have been searching the web for something related to this but cant.</p>
  
  <p>I have this aggreation</p>
  
  <pre><code> Place.aggregate(
      [
          { "$geoNear": {
              "near": {
                  "type": "Point",
                  "coordinates": [longitude, latitude]
              },
              "spherical": true,
              "distanceField": "distance"
          }},
          { $group:
          {   _id: "$_id",
              name: { '$first': '$name' },
              distance: { $first: "$distance" }
          }
          },
          { $project : {
              name: 1,
              distance: 1,
          }}
      ],
      function(error, places) {
          if (error) return callback(error, null);
          callback(null, places)
      }
  );
  </code></pre>
  
  <p>It works, but the geoNear sorting is lost!</p>
  
  <p>But this gives me properly sorted documents:</p>
  
  <pre><code>    Place.aggregate(
      [
          { "$geoNear": {
              "near": {
                  "type": "Point",
                  "coordinates": [longitude, latitude]
              },
              "spherical": true,
              "distanceField": "distance"
          }}
      ],
      function(error, places) {
          if (error) return callback(error, null);
          callback(null, places)
      }
  );
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p>To give you an idea of what im trying to do here is the full query im using </p>
  
  <pre><code>    Place.aggregate(
      [
          { "$geoNear": {
              "near": {
                  "type": "Point",
                  "coordinates": [longitude, latitude]
              },
              "spherical": true,
              "distanceField": "distance"
          }},
          {"$unwind": "$participants" } ,
          { $group:
          {   _id: "$_id",
              name: { '$first': '$name' },
              distance: { $first: "$distance" },
              person: { $sum: 1 },
              sumof:{ $sum: "$participants.age"}
          }
          },
          { $project : {
              name: name,
              distance: 1,
              person: 1,
              meanAge:{ $divide: [ "$sumof", "$person" ]}
          }}
      ],
      function(error, places) {
          if (error) return callback(error, null);
          callback(null, places)
      }
  );
  </code></pre>