【问题标题】:Loopback where filter geo near query [error: unable to find index for $geoNear query]Loopback where filter geo near query [错误:无法找到 $geoNear 查询的索引]
【发布时间】:2015-10-12 05:20:18
【问题描述】:

我正在尝试计算半径内的用户(亲信)。我正在使用 mongodb + loopback 并且可以使用远程方法成功按位置查询。

然而,当我尝试通过 api explorer 在 where 参数中提供附近时,我收到如下错误:

以“where”形式提供的参数:

        {"last_location": {"near": [-33.8670522,151.1957362]}, "maxDistance":1}

请求网址:

http://localhost:3000/api/Cronies/count?where=%7B%22last_location%22%3A%20%7B%22near%22%3A%20%5B-33.8670522%2C151.1957362%5D%7D%2C%20%22maxDistance%22%3A1%7D&access_token=AYlOjRtu77JWNywHVMtRqEAZfLzKNyq2OFV31ZKHaUSQBNZojxSJb9Q5QI7RjAXc

错误:

{
  "error": {
    "name": "MongoError",
    "status": 500,
    "message": "error processing query: ns=fresh.Crony limit=0 skip=0\nTree: $and\n    maxDistance == 1\n    GEONEAR  field=last_location maxdist=1.79769e+308 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query",
    "ok": 0,
    "errmsg": "error processing query: ns=fresh.Crony limit=0 skip=0\nTree: $and\n    maxDistance == 1\n    GEONEAR  field=last_location maxdist=1.79769e+308 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query",
    "code": 2,
    "stack": "MongoError: error processing query: ns=fresh.Crony limit=0 skip=0\nTree: $and\n    maxDistance == 1\n    GEONEAR  field=last_location maxdist=1.79769e+308 isNearSphere=0\nSort: {}\nProj: {}\n planner returned error: unable to find index for $geoNear query\n    at Function.MongoError.create (/Users/alex/dev/crony/crony-backend/node_modules/loopback-connector-mongodb/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:31:11)\n    at /Users/alex/dev/crony/crony-backend/node_modules/loopback-connector-mongodb/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:754:66\n    at /Users/alex/dev/crony/crony-backend/node_modules/loopback/node_modules/continuation-local-storage/context.js:74:17\n    at bound (domain.js:250:14)\n    at runBound (domain.js:263:12)\n    at Callbacks.emit (/Users/alex/dev/crony/crony-backend/node_modules/loopback-connector-mongodb/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:95:3)\n    at null.messageHandler (/Users/alex/dev/crony/crony-backend/node_modules/loopback-connector-mongodb/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:243:23)\n    at Socket.<anonymous> (/Users/alex/dev/crony/crony-backend/node_modules/loopback-connector-mongodb/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:262:22)\n    at emitOne (events.js:77:13)\n    at Socket.emit (events.js:169:7)"
  }
}

【问题讨论】:

    标签: node.js mongodb loopbackjs


    【解决方案1】:

    我认为您不会生成索引列,这很简单,只需连接您的mongo shell 并使用:

    db.<collection>.ensureIndex({point:"2dsphere"})
    

    来源:看that link

    我遇到了同样的问题,这个答案对我有用。

    【讨论】:

    • 确保如果您的集合中的地理位置字段未调用point,则您更改上述索引生成调用以匹配它。另外,如果2dsphere 不适合您,请尝试2d 索引类型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-06
    • 1970-01-01
    • 2014-06-05
    • 2023-04-01
    • 2017-03-24
    • 2021-10-28
    相关资源
    最近更新 更多