大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。
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 ] } } } )
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/118844.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...