为什么2D索引不起作用?

时间:2016-05-07 19:21:31

标签: mongodb performance indexing geolocation mongodb-query

我有一大堆这样的文件:

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

1 个答案:

答案 0 :(得分:0)

解决方案非常简单:

  

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

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