首页 > 实用mongoose查询经纬度距离,返回的数据超过maxdistance?

实用mongoose查询经纬度距离,返回的数据超过maxdistance?

代码如下:


location:{type:[Number],index: '2d'},

Shop.find({location:{$near:loc,$maxDistance: 5}}).limit(50).exec(function(err, doc) {
      if (err) {
        console.log(err);
      }

      callback(doc);
    });
    
    

maxDistance:5 代表查询距离此经纬度5km范围的坐标,返回的结果之中有满足条件的值(小于5km)但是也有大于5km的值,而且有大很多的,请问是哪里出了错呢?

完整的代码如下:

module.exports = function( mongoose) {
  var ShopSchema = new mongoose.Schema({
    shopName:     { type: String, unique: true },
    address:     { type: String},
    location:{type:[Number],index: '2d'},
    shopPicUrl:      {type: String},
    shopPicTrueUrl:{type: String},
    mark:  { type: String},
    open:{type:Boolean},
    shopType:{type:String},
    dish:   {type: [{
      dishName: { type: String},
      tags: { type: Array},
      price: { type: Number},
      intro: { type: String},
      dishPic:{ type: String},
      index:{type:Number}
    }]}

  });

  var Shop = mongoose.model('Shop', ShopSchema);

  var createShop = function(shopName, address,location, shopPicUrl, open,shopType,res, callback) {
    var shopInstance = new Shop({
      shopName: shopName,
      address: address,
      location: location,
      shopPicUrl: shopPicUrl,
      open:open,
      shopType:shopType
      //shopPicTrueUrl:shopPicTrueUrl
    });
    shopInstance.save(function(err){
      callback(err);
    });
  };
  return {
    createShop: createShop
  }
}
【热门文章】
【热门文章】