【问题标题】:I couldn't able to fetch the records by using latitude and longitude, using Mongodb我无法通过使用纬度和经度来获取记录,使用 Mongodb
【发布时间】:2018-07-11 15:45:05
【问题描述】:

我正在尝试使用 mongodb 获取基于纬度和经度的记录。我经历了很多例子,但对我没有任何帮助。

这是我的架构结构。

latlang: {
  type: { type: String, default: 'Point'},
  coordinates: [Number]
}

存储值后的样子

latlang:{
  type: "Point",
  coordinates: [77.6836, 12.8486]
}

我正在为 latlang 字段设置索引,例如

schema.index({ 'latlang.coordinates' : '2dsphere' });

我有多个记录,如下所示

latlang:{
  type: "Point",
  coordinates: [77.6836, 12.8486]
}

latlang:{
  type: "Point",
  coordinates: [77.5712, 12.9766]
} 

 latlang:{
  type: "Point",
  coordinates: [77.6174, 12.9226]
} 

当我尝试使用以下查询获取记录时,我得到空响应

User.find({ loc:{ $near:{ $geometry: {type: "Point" , coordinates:[77.6974,12.9591] }, $maxDistance:10000 }}}).exec();

任何帮助将不胜感激。提前致谢!

【问题讨论】:

  • 您定义了一个latlang 索引和架构,但在您的查询中,您正在过滤一个loc 字段。我认为这可能是一个错字。

标签: mongodb


【解决方案1】:

为 latlang 创建索引: db.<schema>.createIndex({"latlang":"2dsphere"});

然后进行类似的查询

User.find({
latlang: {
    $near: {
        $geometry: {
            type: "Point",
            coordinates: coords
        },
        $maxDistance: 10000
    }
}
})

希望这会有所帮助!

【讨论】:

猜你喜欢
  • 2011-01-05
  • 2015-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-29
相关资源
最近更新 更多