MongoDB / Mongoose geoNear返回不正确的距离

时间:2016-03-13 15:52:26

标签: node.js mongodb mongoose distance

当我尝试使用geoNear()方法查找具有已定义地理坐标的文档时,MongoDB对距离的计算似乎已关闭,从而导致结果为空数列。

我的位置集合中有以下文档:

{
    "_id" : ObjectId("56e4791244ea3215c8537425"),
    "name" : "Cool Place",
    "address" : "224 West 71th Street, New York, NY 10023",
    "rating" : 5,
    "facilities" : [...etc...],
    "coords" : [
        -73.983342,
        40.778183
    ],
    "openingTimes" : [...etc...],
    "reviews" : [...etc...]
}

我的架构如下所示:

var locationSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  address: String,
  rating: {
    type: Number,
    'default': 0,
    min: 0,
    max: 5
  },
  facilities: [String],
  coords: {
    type: [Number],
    index: '2dsphere'
  },
  openingTimes: [openingTimeSchema],
  reviews: [reviewSchema]
});

我的控制器的相关部分如下所示:

var theEarth = (function () {
  var earthRadius = 6371; // km, miles is 3959

  var getDistanceFromRads = function (rads) {
    return parseFloat(rads * earthRadius);
  };

  var getRadsFromDistance = function (distance) {
    return parseFloat(distance / earthRadius);
  };

  return {
    getDistanceFromRads: getDistanceFromRads,
    getRadsFromDistance: getRadsFromDistance
  };
})();

var buildResultsList = function (results) {
  var resultsList = [];

  results.forEach(function (doc) {
    resultsList.push({
      distance: theEarth.getDistanceFromRads(doc.dis),
      name: doc.obj.name,
      address: doc.obj.address,
      rating: doc.obj.rating,
      facilities: doc.obj.facilities,
      coords: doc.obj.coords,
      _id: doc.obj._id
    });
  });

  return resultsList;
};

module.exports.locationsListByDistance = function (req, res) {
  var lng = parseFloat(req.query.lng);
  var lat = parseFloat(req.query.lat);
  var maxDistance = parseFloat(req.query.maxDistance);

  var point = {
    type: 'Point',
    coordinates: [lng, lat]
  };

  var geoOptions = {
    spherical: true,
    maxDistance: theEarth.getRadsFromDistance(maxDistance),
    num: 10
  };

  Loc.geoNear(point, geoOptions, function (err, results, stats) {
    sendJsonResponse(res, 200, buildResultsList(results));
  });
};

最后,这是我正在提出的要求:

http://localhost:3000/api/locations?lng=-73.9827568&lat=40.7791425&maxDistance=10

但是,我只得到一个空的结果数组,即使该位置在我在请求中指定的坐标的10公里范围内。如果我将maxDistance查询参数更改为100000,则返回结果,显示距离为749557.1657218104 km:

[{"distance":749557.1657218104,"name":"Cool Place","address":"224 West 71th Street, New York, NY 10023","rating":5,"facilities":["Hot drinks","Premium wifi"],"coords":[-73.983342,40.778183],"_id":"56e4791244ea3215c8537425"}]

我使用http://www.movable-type.co.uk/scripts/latlong.html来计算这两组坐标之间的距离,并看到它们相距仅0.1175公里。

另一个注意事项:如果我在查询中使用与位置关联的精确坐标(-73.983342,40.778183),则返回位置IS,距离正确为0 km。

成功请求:

http://localhost:3000/api/locations?lng=-73.983342&lat=40.778183&maxDistance=10

返回:

[{"distance":0,"name":"Cool Place","address":"224 West 71th Street, New York, NY 10023","rating":5,"facilities":["Hot drinks","Premium wifi"],"_id":"56e4791244ea3215c8537425"}]

任何想法我在这里做错了什么?谢谢!

1 个答案:

答案 0 :(得分:0)

您使用的是什么版本的Mongoose?尝试使用“mongoose”:“~3.8.20”并更新npm包并重试。