使用地理空间查询根据距离查找mongodb中的用户

时间:2016-08-13 08:22:45

标签: mongodb geolocation mongodb-query

我正在开展一些项目,在这个项目中我必须找到一个特定区域的用户,例如大于20公里且小于40公里,我已经为此编写了查询

query.location = {
  $nearSphere: 
  {
    $geometry: 
    {
      type:'Point',
      coordinates: 
      [
        bookingData.bookingAddress.location.coordinates[0],
        bookingData.bookingAddress.location.coordinates[1]
      ]
    },
    $minDistance: (20*1000),
    $maxDistance: (40*1000)  // this distance should be in meters (in our case I have set 40 km)
  }
};

但即使我的数据库中有纬度和经度的用户,结果也没有显示任何内容。

当我运行查询

query.location = {
  $nearSphere:
  {
    $geometry:
    {
      type:'Point',
      coordinates:
      [
        bookingData.bookingAddress.location.coordinates[0],
        bookingData.bookingAddress.location.coordinates[1]
      ]
    },
    $maxDistance: (40*1000)  // this distance should be in meters (in our case I have set 40 km)
  }
};

显示正确的结果。

这是我为存储位置而制作的模型:

bookingAddress : {
        location : {
            'type':{type:String,enum:CONST.GEO_JSON_TYPES.Point,default: CONST.GEO_JSON_TYPES.Point},
            coordinates: {type: [Number], default: [0, 0],required: true}
        },
        address : {type : String,default:null}
    },

我哪里错了?

0 个答案:

没有答案