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]}}
]);

地理空間索引

2dsphereide

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

查詢

指定位置最近的點code

db.location.findOne({"location":{$nearSphere:{$geometry:{type:"Point",coordinates:[113,23]}}}});
相關文章
相關標籤/搜索