【问题标题】:Getting Error while fetching data location wise with $near query in mongoose在猫鼬中使用 $near 查询明智地获取数据位置时出错
【发布时间】:2020-05-31 15:04:08
【问题描述】:
query = {
  "location" : {
       "$near" : {
                    "$geometry": {
                        "type": "Point" ,
                        "coordinates": [18.55,73.78]
                    }
                }
            }
       }
  }






planner returned error: unable to find index for $geoNear query
    at Connection.<anonymous> (D:\Projects\api\node_modules\mongodb-core\lib\connection\pool.js:443:61)
    at Connection.emit (events.js:198:13)
    at Connection.EventEmitter.emit (domain.js:466:23)
    at processMessage (D:\Projects\api\node_modules\mongodb-core\lib\connection\connection.js:364:10)
    at Socket.<anonymous> (D:\Projects\api\node_modules\mongodb-core\lib\connection\connection.js:533:15)
    at Socket.emit (events.js:198:13)
    at Socket.EventEmitter.emit (domain.js:466:23)
    at addChunk (_stream_readable.js:288:12)
    at readableAddChunk (_stream_readable.js:269:11)
    at Socket.Readable.push (_stream_readable.js:224:10)
    at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
  ok: 0,

为此,我在以下链接中指定的位置字段上创建了 2dsphere

https://docs.mongodb.com/manual/core/2dsphere/

db.collectionName.createIndex( { location : "2dsphere" } )

如果我做错了什么,请帮助我理解。

【问题讨论】:

  • 尝试将 $near 更改为 $nearSphere 并在 $geometry 旁边添加 $minDistance: 0$maxDistance: yourmaxdistance 如果它仍然不起作用
  • 感谢@AshwynHorton 的信息。我也尝试过使用$nearSphere ......但仍然遇到同样的错误。
  • 酷酷,如果对您有帮助,请告诉我...
  • 是的.. 尝试了这个 $nearSphere.. 但没有工作.. 仍然得到同样的错误..

标签: node.js mongodb mongoose geolocation geometry


【解决方案1】:

尝试将$near 更改为$nearSphere 并在$geometry 旁边添加$minDistance: 0$maxDistance: yourmaxdistance,如果它仍然不起作用......所以它看起来像

query = {
  location: {
     $nearSphere: {
        $geometry: {
            type: "Point" ,
            coordinates: [18.55,73.78],
        },
        $minDistance: 0,
        $maxDistance: //your max will go here
     }
  }
}

如果上述方法不起作用,请尝试再次创建索引并留意输出...

这是一个无效的索引创建示例...

{
        "ok" : 0,
        "errmsg" : "Unknown index plugin '2dSphere'",
        "code" : 67,
        "codeName" : "CannotCreateIndex",
        "operationTime" : Timestamp(1590940517, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1590940517, 1),
                "signature" : {
                        "hash" : BinData(0,""),
                        "keyId" : NumberLong("")
                }
        }
}

【讨论】:

  • 尝试了这个 $nearSphere 以及 minDistance 和 maxDistance 但因为它是可选的所以给出同样的错误..
  • 好的,您是否尝试过再次创建索引...从创建索引中获得的输出确认已创建...让我编辑以显示示例
  • 是的,伙计,我创建了索引。我也检查过 - {“v”:2,“key”:{“location”:“2dsphere”},“name”:“location_2dsphere”,“ns”:“DEV_DB.items”,“2dsphereIndexVersion”:3 }
猜你喜欢
  • 2017-04-09
  • 1970-01-01
  • 1970-01-01
  • 2022-11-13
  • 2013-04-16
  • 2015-11-18
  • 1970-01-01
  • 1970-01-01
  • 2020-06-28
相关资源
最近更新 更多