前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >存储MongoDb的经纬度数据实现空间索引

存储MongoDb的经纬度数据实现空间索引

作者头像
HHTjim 部落格
发布2022-09-26 10:50:15
6130
发布2022-09-26 10:50:15
举报
文章被收录于专栏:HHTjim'S 部落格

存储MongoDb的经纬度数据实现空间索引

作者:matrix 被围观: 3,727 次 发布时间:2018-11-26 分类:零零星星 | 无评论 »

这是一个创建于 1375 天前的主题,其中的信息可能已经有所发展或是发生改变。

数据存储

mongoDb自带了地理空间索引,使用2dsphere索引类型 可以存储GeoJSON格式数据 指定点、线和多边形。

mongodb数据

代码语言:javascript
复制
{
    "_id" : ObjectId("5bf04829c6671064f9039dd0"), 
    "order_info" : {
        "survey_coordinate" : {
            "type" : "Point", 
            "coordinates" : [
                105.431, 
                42.009
            ],
            "name":"XXXXX街道办"
        }
    }
}

order_info.survey_coordinate字段为需要存储的数据

type:Point 点

coordinates 经纬度信息: longitude, latitude代表经度, 纬度

name为附加自定的存储名称字段

参考:

http://geojson.org/

mongodb官方文档:https://docs.mongodb.com/manual/reference/geojson/

添加索引

手动创建

代码语言:javascript
复制
db.order.ensureIndex( {"order_info.survey_coordinate" : "2dsphere"} )
//联合索引 如:  db.order.createIndex( {"order_info.survey_coordinate" : "2dsphere","category" : -1, name: 1 } )
// 指定索引名称:db.order.createIndex( {"order_info.survey_coordinate" : "2dsphere"},{name:"locc"} ) 

参考:

http://www.runoob.com/mongodb/mongodb-indexing.html

代码创建

后来专门去找了下php下代码的操作,翻看各种TP文档无果,还是Google靠谱。php官方文档中

ThinkPHP5.0 参考

代码语言:javascript
复制
        $mongodb;//mongodb对象为 think\db\Connection  https://www.kancloud.cn/manual/thinkphp5/167865 
        //创建索引
        $command = new \MongoDB\Driver\Command([
            'createIndexes' => 'order',//给哪个集合创建索引
            'indexes' => [[
                'name' => 'order_info_loc_2dsphere',//索引名称
                'key'  => ['order_info.survey_coordinate' => '2dsphere']//索引的字段  索引类型
            ]]
        ]);
       $result =  $mongodb->command($command);

       //首次创建返回数据
//        /vagrant/saasSystem/thinkphp/library/think/Debug.php:193:
//array (size=1)
//  0 =>
//    array (size=4)
//      'createdCollectionAutomatically' => boolean false
//      'numIndexesBefore' => int 1
//      'numIndexesAfter' => int 2
//      'ok' => float 1
//0.322759s ShowPageTrace

       //重复创建返回数据
//        /vagrant/saasSystem/thinkphp/library/think/Debug.php:193:
//array (size=1)
//  0 =>
//    array (size=5)
//      'createdCollectionAutomatically' => boolean false
//      'numIndexesBefore' => int 2
//      'numIndexesAfter' => int 2
//      'note' => string 'all indexes already exist' (length=25)
//      'ok' => float 1
       halt($result);

参考:

http://php.net/manual/en/mongocollection.createindex.php#119584

数据查询

runCommand执行

代码语言:javascript
复制
db.runCommand( {
   geoNear: "order" , //存在2dsphere索引的集合名称
   near: { type: "Point" , coordinates: [118.783799, 31.979234] } ,
   spherical: true,
   limit:1,
}) 

find

代码语言:javascript
复制
db.order.find({"order_info.survey_coordinate" : { "$near" : { "$geometry" : { "type" : "Point", "coordinates" : [118.783799, 31.979234] } } } })

参考:

https://blog.csdn.net/u014230597/article/details/52635190

https://blog.csdn.net/medea_yang/article/details/53436460

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 存储MongoDb的经纬度数据实现空间索引
    • 数据存储
      • 添加索引
        • 数据查询
        相关产品与服务
        对象存储
        对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档