简体   繁体   English

如何修复MongooseJS排序和跳过

[英]How to fix MongooseJS sort and skip

I'm working on an Express API using mongoose for MongoDB. 我正在使用MongoDB for MongoDB开发Express API。 In the code below I'm trying to find GeoJson objects that are within a certain distance, Sort them so that the newest objects appear first, skip to a certain document before returning the specified number of objects in the "limit". 在下面的代码中,我试图找到在一定距离内的GeoJson对象,对它们进行排序,以使最新的对象首先出现,跳至某个文档,然后在“限制”中返回指定数量的对象。

// 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();
  });
});

When I run the code the geoNear() function works perfectly as does the limit function. 当我运行代码时,geoNear()函数和limit函数都可以正常工作。 My issues are with sort and skip. 我的问题是排序和跳过。 It is almost as if sort and skip are ignored. 几乎就像忽略了sort和skip一样。 I have looked at the mongo and mongoose docs as well as similar code online and I can't find a fix anywhere. 我在网上看过mongo和mongoose文档以及类似的代码,但在任何地方都找不到修复程序。 How can I make it so that when I try to get the documents in a certain area my results are properly skipped over and organized by date? 我如何做到这一点,以便在尝试将文档放在特定区域中时,可以按日期正确跳过和整理结果?

Can you try changing this: 您可以尝试更改以下内容吗?

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

to the following: 到以下内容:

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

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

I think sort, skip, and limit need to be called directly and not specified in the options. 我认为排序,跳过和限制需要直接调用,而不是在选项中指定。

I fixed the issue yesterday. 我昨天解决了这个问题。 The code is posted below. 该代码发布在下面。

// 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();
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM