MongoDB 空间定位(点) 与 距离检索
2016-04-12 14:18
639 查看
转自: http://blog.csdn.net/flamingsky007/article/details/39208837
基于 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 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米以内数据
相关文章推荐
- php yii框架使用MongoDb
- 在MongoDB中实现聚合函数(mapreduce)
- mongoDB-java
- MongoDB3.0添加用户认证
- MongoDB基本命令用
- 【MongoDB】WriteConcen写顾虑
- spring security:基于MongoDB的认证
- 【MongoDB】windows下搭建Mongo主(Master)/从(slave)数据库同步
- windows下mongodb安装与使用整理
- MongoDB分片及副本集配置说明
- MongoDB基本使用
- MongoDB快速入门(四)- 插入文档
- MongoDB快速入门(三)- 集合
- MongoDB快速入门(二)- 数据库
- MongoDB快速入门(一)
- 13、MongoDB的分片,以及分布式的实现(分片,拆分的是集合,不是单条数据)
- 12、MongoDB的副本集( ReplSet )集群
- mongoDB使用复制还原数据库节省空间
- 11、mongodb的主从复制集群
- mongodb unset/set 删除/增加字段