Mongodb地理空间索引
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 ] } } } )
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。