我正在创建一个 2dsphere 索引并尝试将它用于我的地理空间查询。但是,我发现当我使用 $geoWithin.$box 时,它不使用索引,因此非常慢。如果我使用 $geoWithin.$geometry,则将使用索引。该文件说,$箱支撑指数,所以我一定是错过了什么。任何的想法?
{
"v" : 1,
"key" : {
"details.lonlat" : "2dsphere"
},
"name" : "longlat",
"ns" : "realestate.property",
"2dsphereIndexVersion" : 2
}
Run Code Online (Sandbox Code Playgroud)
> 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"
},
...
Run Code Online (Sandbox Code Playgroud)
> 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"
Run Code Online (Sandbox Code Playgroud)
"version" : "3.0.4",
"gitVersion" : "0481c958daeb2969800511e7475dc66986fa9ed5"
Run Code Online (Sandbox Code Playgroud)
2dsphere 不支持 $box 查询。这就是为什么您的查询属于完整集合扫描的原因。
盒子文档说明如下:
Only the 2d geospatial index supports $box
Run Code Online (Sandbox Code Playgroud)
添加 2d 索引应该可以解决问题,例如:
db.property.ensureIndex({"details.lonlat": "2d"});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
495 次 |
| 最近记录: |