Mongodb地理空间索引不支持$box? [英] Mongodb Geospatial index does not support $box?
问题描述
我正在创建一个 2dsphere 索引并尝试将其用于我的地理空间查询.但是,我发现当我使用 $geoWithin.$box 时,它不使用索引,因此非常慢.如果我使用 $geoWithin.$geometry,则将使用索引.document 说 $box 支持索引,所以我必须想念一些东西.有什么想法吗?
I am creating a 2dsphere index and trying to utilize it for my geospatial queries. However, I find when I use $geoWithin.$box, it does not use the index, hence, very slow. If I use $geoWithin.$geometry, then the index will be used. The document says $box supports index, so I must be miss something. Any idea?
{
"v" : 1,
"key" : {
"details.lonlat" : "2dsphere"
},
"name" : "longlat",
"ns" : "realestate.property",
"2dsphereIndexVersion" : 2
}
使用 GeoJSON 多边形查询使用索引
> db.property.find({'details.lonlat': {'$geoWithin': {$geometry: {type:'Polygon', coordinates: [[[1,1],[2,2],[3,3], [1,1]]]}}}}).explain()
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"details.lonlat" : "2dsphere"
},
...
使用 $box 不使用索引,而是使用集合扫描(为什么?)
> db.property.find({'details.lonlat': {'$geoWithin': {'$box': [[ 0, 0 ], [ 100, 100 ] ]}}}).explain()
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"details.lonlat" : {
"$geoWithin" : {
"$box" : [
[
0,
0
],
[
100,
100
]
]
}
}
},
"direction" : "forward"
Mongodb 信息
"version" : "3.0.4",
"gitVersion" : "0481c958daeb2969800511e7475dc66986fa9ed5"
推荐答案
2dsphere 不支持 $box 查询.这就是为什么您的查询属于完整集合扫描的原因.
The 2dsphere does not support $box query. That's why your query falls to a full collection scan.
框文档声明如下:>
The box documentation states the following:
Only the 2d geospatial index supports $box
添加 2d 索引应该可以解决问题,例如:
Adding a 2d index should do the trick, something like:
db.property.ensureIndex({"details.lonlat": "2d"});
这篇关于Mongodb地理空间索引不支持$box?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!