2

我很难处理应该是微不足道的事情......
我有以下个人资料文档结构:

 {
   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 版。

我究竟做错了什么?非常感谢您的回答。

4

1 回答 1

6

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

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

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

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

这是一个工作示例:

我创建了三个配置文件对象,位置为数组,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"
    }
]
于 2014-01-07T14:32:38.997 回答