【问题标题】:GeoNear Query giving error unable to find index for $geoNear query"GeoNear 查询给出错误无法找到 $geoNear 查询的索引”
【发布时间】:2017-05-02 23:45:30
【问题描述】:

我正在尝试对 geonear 查询进行 POC,但我得到了

 Error: error: {
    "ok" : 0,
    "errmsg" : "error processing query: ns=zircon.storeTree: GEONEAR  field=start maxdist=0.045045 isNearSphere=1\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query",
    "code" : 2

我也创建了我的基本索引。

> db.store.getIndexes();                                   
        [                                                          
                {                                                  
                        "v" : 1,                                   
                        "key" : {                                  
                                "_id" : 1                          
                        },                                         
                        "name" : "_id_",                           
                        "ns" : "zircon.store"                      
                },                                                 
                {                                                  
                        "v" : 1,                                   
                        "key" : {                                  
                                "point" : "2dsphere"               
                        },                                         
                        "name" : "point_2dsphere",                 
                        "ns" : "zircon.store",                     
                        "2dsphereIndexVersion" : 3                 
                },                                                 
                {                                                  
                        "v" : 1,                                   
                        "key" : {                                  
                                "location.Point" : "2dsphere"      
                        },                                         
                        "name" : "location.Point_2dsphere",        
                        "ns" : "zircon.store",                     
                        "2dsphereIndexVersion" : 3                 
                }                                                  
        ]   

我的模特是

```json
{
  "_id": ObjectId("57b4d4437ab5db070e8cc02d"),
  "location": {
    "Point": {
      "x": 47.629104999999996,
      "y": 77.21983919
    }
  },
  "name": "Gulati XX :19",
  "createdDate": ISODate("2016-08-17T21:16:51.605Z"),
  "modifiedDate": ISODate("2016-08-17T21:16:51.605Z")
}
```

请就我在这里做错了什么提出建议。是什么导致 geonear 仍然无法工作。

【问题讨论】:

    标签: mongodb mongoose


    【解决方案1】:

    有同样的错误,问题是我在“location.coordinates”字段而不是“location”字段上创建了索引。这在我的流星开发服务器中运行良好,但一旦推送到登台服务器,数据库就找不到索引。

    在创建 2dspere 索引时,您应该使用“复合”方法并按照 mongo 文档在父位置字段上执行此操作:https://docs.mongodb.com/manual/core/2dsphere/#id1

    鉴于您的 json 文件和数据库名称,我会尝试以下操作:db.store.createIndex({"location": "2dspere"}) 或者 db.store.ensureIndex({"location": "2dspere"})

    【讨论】:

      【解决方案2】:

      您需要为位置值提供索引。

      例子:

      { location : { $near : [ -73.9667, 40.78 ], $maxDistance: 0.10 } }
      

      【讨论】:

        猜你喜欢
        • 2015-01-06
        • 2014-06-05
        • 1970-01-01
        • 2023-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-24
        • 2021-10-28
        相关资源
        最近更新 更多