我有一个集合名称locations
,其数据结构如下:
{
"_id" : ObjectId("4e95263f1783ae8487be26d4"),
"name" : "test 1",
"location" : {
"coordinate" : {
"latitude" : 40.731987,
"longitude" : -73.999701
},
"address": "xxxxxxx"
}
}
并且想要针对 location.coordinate
字段进行地理查询。
当我尝试添加索引时,我得到以下结果:
$> db.locations.ensureIndex( { "location.coordinate" : "2d" } )
$> **** SyntaxError: syntax error (shell):0
是否可以对这种结构使用地理空间索引?
最佳答案
由于mongodb是基于GeoJSON格式,最好先有经度元素
"location" : {
"coordinate" : {
"longitude" : -73.999701,
"latitude" : 40.731987
},
在 mongodb geospatial page ,你可以在多个地方看到它
By default, the index assumes you are indexing longitude/latitude and is thus configured for a [-180..180) value range.
和
The code assumes that you are using decimal degrees in (longitude, latitude) order. This is the same order used for the GeoJSON spec. Using (latitude, longitude) will result in very incorrect results, but is often the ordering used elsewhere, so it is good to double-check. The names you assign to a location object (if using an object and not an array) are completely ignored, only the ordering is detected.
关于mongodb - 内部结构地理空间索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7735905/