我正在尝试获得半径范围内的用户数(Cronies)。我正在使用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)"
}
}
答案 0 :(得分:1)
我认为您不会生成索引列,这很简单,只需连接到您的mongo shell
并使用:
db.<collection>.ensureIndex({point:"2dsphere"})
来源:看that link
我遇到了同样的问题,答案对我有用。