【问题标题】:Why 2D-index not working?为什么二维索引不起作用?
【发布时间】:2016-09-02 17:29:47
【问题描述】:

我有一大堆这样的文件:

{
    "_id" : ObjectId("572e33baf082e29c46cadb7b"),
    "nodes" : [ 
        [ 
            39.2035598754883, 
            51.6601028442383
        ], 
        [ 
            39.2038421630859, 
            51.6602439880371
        ], 
        [ 
            39.2038688659668, 
            51.6602249145508
        ]
    ]
}

我想通过坐标搜索文档。我的查询是:

db.points.find({nodes: {$elemMatch: {$geoWithin: {$box: [[39.1981, 51.660], [39.206, 51.664]]}}}})

我还加了一个索引

db.points.createIndex( { "nodes": "2d" } )

但它没有效果。收藏统计:

{
    "ns" : "base.points",
    "count" : 215583,
    "size" : 61338720,
    "avgObjSize" : 284,
    "storageSize" : 86310912,
    "numExtents" : 10,
    "nindexes" : 3,
    "lastExtentSize" : 27869184,
    "paddingFactor" : 1.0,
    "systemFlags" : 0,
    "userFlags" : 1,
    "totalIndexSize" : 39759888,
    "indexSizes" : {
        "_id_" : 7006832,
        "nodes_2d" : 26719168
    },
    "ok" : 1.0
}

并解释一下:

{
    "cursor" : "BasicCursor",
    "isMultiKey" : false,
    "n" : 61,
    "nscannedObjects" : 215583,
    "nscanned" : 215583,
    "nscannedObjectsAllPlans" : 215583,
    "nscannedAllPlans" : 215583,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 1684,
    "nChunkSkips" : 0,
    "millis" : 1466,
    "server" : "DEVELOP-PC:27017",
    "filterSet" : false
}

【问题讨论】:

    标签: mongodb performance indexing geolocation mongodb-query


    【解决方案1】:

    解决方法很简单:

    db.points.find({nodes: {$geoWithin: {$box: [[39.1981, 51.660], [39.206, 51.664]]}}})

    应从查询$elemMatch 中排除,索引将正常工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 2011-04-06
      • 2015-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多