【问题标题】:Mongodb Geospatial index does not support $box?Mongodb地理空间索引不支持$box?
【发布时间】:2016-09-14 17:49:45
【问题描述】:

我正在创建一个 2dsphere 索引并尝试将其用于我的地理空间查询。但是,我发现当我使用 $geoWithin.$box 时,它不使用索引,因此非常慢。如果我使用 $geoWithin.$geometry,那么将使用索引。 document 说 $box 支持索引,所以我一定想念一些东西。有什么想法吗?

地理空间索引

    {
            "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"

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    2dsphere 不支持 $box 查询。这就是您的查询属于完整集合扫描的原因。

    documentation 框声明如下:

    Only the 2d geospatial index supports $box
    

    添加 2d 索引应该可以解决问题,例如:

    db.property.ensureIndex({"details.lonlat": "2d"});
    

    【讨论】:

      猜你喜欢
      • 2016-02-12
      • 2011-10-23
      • 2014-01-04
      • 2019-11-30
      • 1970-01-01
      • 1970-01-01
      • 2011-04-27
      • 2011-04-22
      • 2016-10-02
      相关资源
      最近更新 更多