最初研究蒙戈 - 以GeoJSON的目的是想在前端地图服务添加一个搜索功能:
目标类型;搜索范围。
这个功能通过前端把与蒙戈通信,在蒙戈中计算并将结果返回到前端,在网页中显示。于是首先从蒙戈操作的入手进行研究。
1.mongo搜索地理位置数据步骤
1.1.添加数据
1)添加以GeoJSON数据
C:\Users\dell>cd C:\Program Files\MongoDB\Server\4.0\bin
>mongoimport -d test -c json --type json C:/Users/dell/Desktop/MongoDB/geojson.json
路径根据自己电脑设置
2)直接在机器人3T中操作
插入文件,将以下数据填进去,点保存。
{
"_id" : 55,
"coordinate" : {
"longitude" : 121.47954,
"latitude" : 31.22911
}
}
{
"_id" : 53,
"coordinate" : {
"longitude" : 121.42485,
"latitude" : 31.24458
}
}
{
"_id" : 56,
"coordinate" : {
"longitude" : 121.55226,
"latitude" : 31.22385
}
}
1.2.搜索
/ *以GeoJSON数据存在geojsonwutou集合中,2)数据存在TEST03集合中* /
/*1*/
db.test03.createIndex({"coordinate":"2d"}) //首先建立空间索引
db.test03.find({
coordinate:{"$near":[130.261,31.729],$maxDistance:10}
})
/*2*/
db.test03.createIndex({coordinate:"2dsphere"})
db.test03.find({
coordinate:{"$nearSphere":[121.773,31.042],$maxDistance:1}
})
/*3*/
//geojson格式要删掉3个一级标签才能正常索引
db.test02.createIndex({geometry:"2dsphere"}) //geojson只能建立2dsphere
db.test02.find(
{
geometry: {
$near:{
$geometry:{
"type" : "Point",
"coordinates" : [121.427788, 31.201160]
},
$maxDistance: 100 //geojson单位是m,其他数据单位是度,经纬度小数点后第六位≈1m
}
}
}
)
必须设置index,否则无法进行搜索操作
2.geojson注意事项
GeoJSON的文件通过shape文件转换得到,
然后删除生成JSON文件的前四行和最后两行
{
"_id" : ObjectId("5b83de57013ba150921eb001"),
"type" : "FeatureCollection",
"features" : [
......
......
......
]
}