Mongo 3.0、地理空间数据与计算距离
Mongo 3.0, geospatial data and calculating distance
目前我又花了一个小时试图弄清楚我的查询出了什么问题。
使用 Mongo 版本 3.0 我检查了两个不超过 17000 米的点。不幸的是,在尝试使用高达 18000 米的距离从数据库中获取它们时,我什么也没得到。
我试过“2dsphere”and/or“2d”索引:
db.location.ensureIndex( { position : "2dsphere" } )
或
db.location.ensureIndex( { position : "2d" } )
然后我与:
一起工作
db.location.find( {
position: { $near: [ 40.705395, -73.891104 ], $maxDistance: 18000 }
} )
或
db.location.find( {
position:
{
$near : {
$geometry : {
type : "Point" ,
coordinates : [ 40.705395, -73.891104 ]
},
$maxDistance : 18000
}
}
})
并与:
db.location.find( { location : { $near : [ 40.705395, -73.891104 ], $maxDistance: 18000 } } )
我有:
Error: error: {
"$err" : "Unable to execute query: error processing query: ns=distance.location limit=0 skip=0\nTree: GEONEAR field=location maxdist=18000 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query",
"code" : 17007
}
示例文档:
> db.location.find()
{ "_id" : ObjectId("5504ba8a97b3b27d56dc4045"), "id" : 123, "position" : [ 40.705562, -73.898818 ] }
坦白说我找不到我的问题出在哪里。数据库的计算似乎是错误的,但我宁愿说我犯了某种错误。
你遇到过类似的情况吗?
您需要在 collection 上创建一个 2DSphere index 才能执行地理空间查询。
db.location.ensureIndex({position:"2dsphere"});
此外,我注意到您将坐标存储为 [ 40.705562, -73.898818 ]。在MongoDB中,坐标轴顺序为[经度,纬度],根据该顺序您的坐标位于南极某地。您需要存储 [ -73.898818, 40.705562 ] 之类的坐标(除非您确实存储了南极洲的位置)。
目前我又花了一个小时试图弄清楚我的查询出了什么问题。
使用 Mongo 版本 3.0 我检查了两个不超过 17000 米的点。不幸的是,在尝试使用高达 18000 米的距离从数据库中获取它们时,我什么也没得到。
我试过“2dsphere”and/or“2d”索引:
db.location.ensureIndex( { position : "2dsphere" } )
或
db.location.ensureIndex( { position : "2d" } )
然后我与:
一起工作 db.location.find( {
position: { $near: [ 40.705395, -73.891104 ], $maxDistance: 18000 }
} )
或
db.location.find( {
position:
{
$near : {
$geometry : {
type : "Point" ,
coordinates : [ 40.705395, -73.891104 ]
},
$maxDistance : 18000
}
}
})
并与:
db.location.find( { location : { $near : [ 40.705395, -73.891104 ], $maxDistance: 18000 } } )
我有:
Error: error: {
"$err" : "Unable to execute query: error processing query: ns=distance.location limit=0 skip=0\nTree: GEONEAR field=location maxdist=18000 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query",
"code" : 17007
}
示例文档:
> db.location.find()
{ "_id" : ObjectId("5504ba8a97b3b27d56dc4045"), "id" : 123, "position" : [ 40.705562, -73.898818 ] }
坦白说我找不到我的问题出在哪里。数据库的计算似乎是错误的,但我宁愿说我犯了某种错误。
你遇到过类似的情况吗?
您需要在 collection 上创建一个 2DSphere index 才能执行地理空间查询。
db.location.ensureIndex({position:"2dsphere"});
此外,我注意到您将坐标存储为 [ 40.705562, -73.898818 ]。在MongoDB中,坐标轴顺序为[经度,纬度],根据该顺序您的坐标位于南极某地。您需要存储 [ -73.898818, 40.705562 ] 之类的坐标(除非您确实存储了南极洲的位置)。