大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。
1. LBS地理空间索引
2. 创建索引
db.places.insert(
{
loc : { type: "Point", coordinates: [ -73.97, 40.77 ] },
name: "Central Park",
category : "Parks"
}
)
db.places.insert(
{
loc : { type: "Point", coordinates: [ -73.88, 40.78 ] },
name: "La Guardia Airport",
category : "Airport"
}
)
建立索引
db.places.ensureIndex( { loc : "2dsphere" } )
參数不是1或-1,为2dsphere。
db.places.ensureIndex( { loc : "2dsphere" , category : -1, name: 1 } )
3. 查询
3.1 查询多边形范围的值
db.places.find( { loc :
{ $geoWithin :
{ $geometry :
{ type : "Polygon" ,
coordinates : [ [
[ 0 , 0 ] ,
[ 3 , 6 ] ,
[ 6 , 1 ] ,
[ 0 , 0 ]
] ]
} } } } )
3.2 查询附近的值
db.places.find( { loc :
{ $near :
{ $geometry :
{ type : "Point" ,
coordinates : [ <longitude> , <latitude> ] } ,
$maxDistance : <distance in meters>
} } } )
3.3 查询圆形内的值
db.places.find( { loc :
{ $geoWithin :
{ $centerSphere :
[ [ -88 , 30 ] , 10 ]
} } } )
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/118844.html原文链接:https://javaforall.net
