如何修复MongooseJS排序并跳过

时间:2016-11-03 18:43:45

标签: javascript node.js mongodb sorting mongoose

我使用mongoose为MongoDB开发Express API。在下面的代码中,我试图找到一定距离内的GeoJson对象,对它们进行排序,使最新的对象首先出现,在返回指定数量的对象之前跳到某个文档,然后在" limit& #34 ;.

// Find all of the stacks in a given radius
router.param("range", function(req, res, next, range) {
  if(range === "1") {
    var radius = 8125; // Meters. (5 Miles)
  } else if(range === "0") {
    var radius = 45.75; // Meters. (150 Feet)
  }

  var skipTo = parseInt(req.params.skip);
  var origin = [parseFloat(req.params.lon), parseFloat(req.params.lat)]; // [longitude, latitude]
  var userLocation = { type: "Point", coordinates: origin };
  var options = { maxDistance: radius,
                  spherical : true,
                  sort: {
                     createdAt: -1 // Sort by Date Added DESC
                  },
                  skip: skipTo, // Skip to a specific point
                  limit: 15 // How many returned
                 };

  Stack.geoNear(userLocation, options, function(err, result, stats) {
    if(err) return next(err);

    if(!result) {
      err = new Error("Not Found");
      err.status = 404;
      return next(err);
    }

    req.locals = result;
    next();
  });
});

当我运行代码时,geoNear()函数与limit函数一样完美。我的问题是排序和跳过。这几乎就像忽略了sort和skip一样。我已经在网上查看了mongo和mongoose文档以及类似的代码,我无法在任何地方找到修复程序。我怎样才能这样做,以便当我尝试在某个区域获取文档时,我的结果会被日期正确地跳过和组织?

2 个答案:

答案 0 :(得分:0)

你能尝试改变这个:

Stack.geoNear(userLocation, options, function(err, result, stats) {

以下内容:

var options = { maxDistance: radius, spherical : true };

Stack.geoNear(userLocation, options)
    .sort({CreatedAt: -1})
    .skip(skipTo)
    .limit(15)
    .exec(function(err, result, stats) {

我认为需要直接调用sort,skip和limit,而不是在选项中指定。

答案 1 :(得分:0)

我昨天解决了这个问题。代码发布在下面。

// Find all of the stacks in a given radius
router.param("range", function(req, res, next, range) {

if(range === "1") {
    var radius = 8125; // Meters. (5 Miles)
  } else if(range === "0") {
    var radius = 45.75; // Meters. (150 Feet)
  }

  var skipTo = parseInt(req.params.skip);
  var origin = [parseFloat(req.params.lon), parseFloat(req.params.lat)]; // [longitude, latitude]
  var userLocation = { type: "Point", coordinates: origin };
  var options = { maxDistance: radius,
                  spherical: true,
                  limit: 15 // How many returned
                };

  // Query database for Stack Objects
  Stack.aggregate([
    { '$geoNear': {
        'near': userLocation, // The point where the user is located
        'distanceField': 'dist.calculated', // The distance the Stack Object is from the user
        'maxDistance': radius, // The furthest distance a Stack Object can be from the user
        'spherical': true
      }
    },
    { '$sort': { 'createdAt': -1 } }, // Return newest Stack Object first
    { '$skip': skipTo }, // Paginate
    { '$limit': 15 } // Number of Stacks returned
  ]).exec(function(err, result, stats) {
    if(err) return next(err);

    if(!result) {
      err = new Error("No Stacks Found");
      err.status = 404;
      return next(err);
    }

    req.locals = result;
    next();
  });
});