Mongo复合索引,地理位置不起作用

时间:2014-01-05 09:40:47

标签: mongodb geolocation indexing compound-index

我很难用一些应该是微不足道的事情...... 我有以下个人资料文件结构:

 {
   pid:"profileId",
   loc : {
       "lat" : 32.082156661684621,
       "lon" : 34.813229013156551,
       "locTime" : NumberLong(0)
         }
   age:29
 }

我的应用中常见的用例是检索按年龄过滤的附近配置文件。

   { "loc" : { "$near" : [ 32.08290052711715 , 34.80888522811172] , "$maxDistance" :    179.98560115190784}, "age" : { "$gte" : 0 , "$lte" : 33}}

所以我创建了以下复合索引:

  { 'loc':2d , age:1}

无论我做什么,我都无法使用创建的索引运行查询(也尝试使用提示)
这是查询的生成解释:

  { 
  "cursor" : "GeoSearchCursor" , 
  "isMultiKey" : false , 
  "n" : 4 , 
  "nscannedObjects" : 4 , 
  "nscanned" : 4 , 
  "nscannedObjectsAllPlans" : 4 , 
  "nscannedAllPlans" : 4 , 
  "scanAndOrder" : false , 
  "indexOnly" : false , 
  "nYields" : 0 , 
  "nChunkSkips" : 0 , 
  "millis" : 0 , 
  "indexBounds" : { } ,
  "allPlans" : [ { "cursor" : "GeoSearchCursor" , "n" : 4 , "nscannedObjects" : 4 , "nscanned" :    4 , "indexBounds" : { }
  }

我正在使用mongodb版本2.4.4。

我做错了什么?你的回答非常感谢。

1 个答案:

答案 0 :(得分:5)

解释输出显示“光标”:“GeoSearchCursor”。这表示您的查询使用了地理空间索引。

有关详细信息,请参阅以下内容: http://docs.mongodb.org/manual/reference/method/cursor.explain/

2d索引支持只有一个附加字段的复合索引,作为2d索引字段的后缀。 http://docs.mongodb.org/manual/applications/geospatial-indexes

正如@stennie在你的问题评论中提到的,问题可能是坐标的排序。他们应该订购长,拉特。如果这不起作用,请尝试将loc存储为具有长第一个元素的数组,lat第二个。

这是一个有效的例子:

我创建了三个配置文件对象,其中location为array,locTime与loc分开。

> db.profile.find()
{ "_id" : ObjectId("52cd54f1c43bb3a468b9fd0d"), "loc" : [  -6,  50 ], "age" : 29, "pid" : "001", "locTime" : NumberLong(0) }
{ "_id" : ObjectId("52cd5507c43bb3a468b9fd0f"), "loc" : [  -6,  53 ], "age" : 30, "pid" : "002", "locTime" : NumberLong(1) }
{ "_id" : ObjectId("52cd5515c43bb3a468b9fd10"), "loc" : [  -1,  51 ], "age" : 31, "pid" : "003", "loctime" : NumberLong(2) }

寻找使用大距离和年龄

> db.profile.find({ "loc" : { "$near" : [ -1, 50] , "$maxDistance" : 5}, "age" : { "$gte" : 0 , "$lte" : 33}})
{ "_id" : ObjectId("52cd5515c43bb3a468b9fd10"), "loc" : [  -1,  51 ], "age" : 31, "pid" : "003", "loctime" : NumberLong(2) }
{ "_id" : ObjectId("52cd54f1c43bb3a468b9fd0d"), "loc" : [  -6,  50 ], "age" : 29, "pid" : "001", "locTime" : NumberLong(0) }

说明显示正在使用索引:

> db.profile.find({ "loc" : { "$near" : [ -1, 50] , "$maxDistance" : 5}, "age" : { "$gte" : 0 , "$lte" : 33}}).explain()
{
    "cursor" : "GeoSearchCursor",
    "isMultiKey" : false,
    "n" : 2,
    "nscannedObjects" : 2,
    "nscanned" : 2,
    "nscannedObjectsAllPlans" : 2,
    "nscannedAllPlans" : 2,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 0,
    "indexBounds" : {

    },
}

缩小相同年龄范围的距离

> db.profile.find({ "loc" : { "$near" : [ -1, 50] , "$maxDistance" : 1}, "age" : { "$gte" : 0 , "$lte" : 33}})

这是解释,再次使用索引:

> db.profile.find({ "loc" : { "$near" : [ -1, 50] , "$maxDistance" :     1}, "age" : { "$gte" : 0 , "$lte" : 33}}).explain()
{
    "cursor" : "GeoSearchCursor",
    "isMultiKey" : false,
    "n" : 1,
    "nscannedObjects" : 1,
    "nscanned" : 1,
    "nscannedObjectsAllPlans" : 1,
    "nscannedAllPlans" : 1,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 0,
    "indexBounds" : {

    },
}

以下是索引:

> db.profile.getIndices()
[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "ns" : "test.profile",
        "name" : "_id_"
    },
    {
        "v" : 1,
        "key" : {
            "loc" : "2d",
            "age" : 1
        },
        "ns" : "test.profile",
        "name" : "loc_2d_age_1"
    }
]
相关问题