mongodb地理空间

2021/8/6 19:06:05

本文主要是介绍mongodb地理空间,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

新建地理空间数据

db.location.insert([
    {"name":"广州","location":{"type":"Point","coordinates":[113.23,23.44]}},
    {"name":"上海","location":{"type":"Point","coordinates":[121.23,31.24]}},
    {"name":"北京","location":{"type":"Point","coordinates":[116,39]}},
    {"name":"深圳","location":{"type":"Point","coordinates":[114,22]}},
    {"name":"杭州","location":{"type":"Point","coordinates":[120.1,30.32]}},
    {"name":"成都","location":{"type":"Point","coordinates":[104.23,30.44]}},
    {"name":"武汉","location":{"type":"Point","coordinates":[114.23,30.44]}}
]);

地理空间索引

2dsphere

db.location.createIndex({"location":"2dsphere"});

查询

指定位置最近的点

db.location.findOne({"location":{$nearSphere:{$geometry:{type:"Point",coordinates:[113,23]}}}});


这篇关于mongodb地理空间的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程