gpt4 book ai didi

node.js - 环回其中筛选地理附近查询 [错误 : unable to find index for $geoNear query]

转载 作者:太空宇宙 更新时间:2023-11-04 00:49:58 25 4
gpt4 key购买 nike

我正在尝试获取半径内的用户(亲信)数量。我正在使用 mongodb + 环回,并且可以使用远程方法成功按位置查询。

然而,当我尝试通过 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)"
}
}

最佳答案

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

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

来源:看that link

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

关于node.js - 环回其中筛选地理附近查询 [错误 : unable to find index for $geoNear query],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33073746/

25 4 0