首页 > 数据库技术 > 详细

MongoDB 空间定位(点) 与 距离检索

时间:2014-09-11 19:30:02      阅读:369      评论:0      收藏:0      [点我收藏+]

基于 MongoDB 2.6


GeoJSON 格式

{ "type": "Point", "coordinates": [lon(经度),lat(纬度)]}

测试数据

db.point.test.insert({"address" : "南京 禄口国际机场","loc" : { "type": "Point", "coordinates": [118.783799,31.979234]}})
db.point.test.insert({"address" : "南京 浦口公园","loc" : { "type": "Point", "coordinates": [118.639523,32.070078]}})
db.point.test.insert({"address" : "南京 火车站","loc" : { "type": "Point", "coordinates": [118.803032,32.09248]}})
db.point.test.insert({"address" : "南京 新街口","loc" : { "type": "Point", "coordinates": [118.790611,32.047616]}})
db.point.test.insert({"address" : "南京 张府园","loc" : { "type": "Point", "coordinates": [118.790427,32.03722]}})
db.point.test.insert({"address" : "南京 三山街","loc" : { "type": "Point", "coordinates": [118.788135,32.029064]}})
db.point.test.insert({"address" : "南京 中华门","loc" : { "type": "Point", "coordinates": [118.781161,32.013023]}})
db.point.test.insert({"address" : "南京 安德门","loc" : { "type": "Point", "coordinates": [118.768964,31.99646]}})

一定要加索引

db.point.test.ensureIndex( { loc : "2dsphere" } )

检索规定半径以内数据(单位为米)

{loc:{$near: {$geometry: {type: "Point" ,coordinates: [118.783799,31.979234]},$maxDistance: 5000}}} //5000米以内数据





MongoDB 空间定位(点) 与 距离检索

原文:http://blog.csdn.net/flamingsky007/article/details/39208837

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!