title: mongodb with $match, $skip, $limit and $geoNear
body: <p>I wanted to do this:</p>
  
  <pre><code>model.User.aggregate([
      //step 1 match criteria
      {
          $match: criteria
      },
      //step 2 skip
      { 
          $skip: offset 
      },
      //step 3 limit
      {
          $limit: limit
      },
      //step 4 sort by computed distance
      {
          $geoNear : {
              near: {type: 'Point', coordinates: coords },
              distanceField: 'currentCity.computed_distance',
              includeLocs: 'currentCity.loc',
              spherical: true,
              uniqueDocs: true,
              distanceMultiplier: 3963.2, //convert to miles (this number is the radius of the earth in miles)
          }
      }
  ],function(err,users){
      if (err) return res.error(err);
      if (!users.length) return res.error('no matched criteria');
      res.apiResponse(users);
  });
  </code></pre>
  
  <p>but the documentation of <strong>$geoNear</strong> states: </p>
  
  <blockquote>
    <p>You can only use $geoNear as the first stage of a pipeline.</p>
  </blockquote>
  
  <p>Reading the documentation, i see that i can simply move <code>$match</code> inside of <code>$geoNear</code> via the <code>query</code> option. likewise, <code>$limit</code> may be placed inside of <code>$geoNear</code> via the <code>limit</code> option. the one problem is, there is no equivalent for the <code>$skip</code> option, so it looks as if facilitating pagination would not be possible? i'm really confused here, why <code>$geoNear</code> can't be the 4th step in the pipeline. The goal of the query is simply to find the best <code>n</code> matches, where <code>n = limit</code>, then sort by nearest in proximity. is this even possible? I am having trouble finding an answer for this specific use case.</p>
  
  <p>I suppose one solution might be to perform a query to select only ids matching documents, convert to list of ids, then do the aggregates with an <code>$in</code> query like so:</p>
  
  <pre><code>model.User.find(criteria).skip(offset).limit(limit).select('_id').exec(function (err, userIds) {
      var ids = [];
      userIds.forEach(function(u){
          ids.push(u._id);
      });
  
      model.User.aggregate([
          {
              $geoNear : {
                  query: { _id: {$in: $ids } },
                  near: {type: 'Point', coordinates: coords },
                  distanceField: 'currentCity.computed_distance',
                  includeLocs: 'currentCity.loc',
                  spherical: true,
                  uniqueDocs: true,
                  distanceMultiplier: 3963.2, //convert to miles (this number is the radius of the earth in miles)
              }
          }
      ],function(err,users){
          if (err) return res.error(err);
          if (!users.length) return res.error('no matched criteria');
          res.apiResponse(users);
      });
  }); 
  </code></pre>
  
  <p>This would work, but ideally i could do it in 1 query if possible. any ideas greatly appreciated.</p>