$near 错误 - 规划器返回错误:无法找到 $geoNear 查询的索引
Posted
技术标签:
【中文标题】$near 错误 - 规划器返回错误:无法找到 $geoNear 查询的索引【英文标题】:$near error - planner returned error: unable to find index for $geoNear query 【发布时间】:2018-09-11 12:57:18 【问题描述】:我在这方面遇到了很大的麻烦。我正在构建一个小 API 来获取给定特定坐标的最接近的存储元素(在 mlab MongoDB 中)。
我想使用 $near 等 Mongo 自定义查询工具从我的数据库中获取最接近的元素。
您可以找到原始仓库here!
数据
原始数据是我转换成geojson格式的一个小.csv。
这是该 .csv 的简短版本:
这是它的geojson版本:
"type": "FeatureCollection",
"features": [
"type": "Feature",
"geometry":
"type": "Point",
"coordinates": [2.4049238868200975, 48.82094216189432]
,
"properties":
"event_type": "imp"
,
"type": "Feature",
"geometry":
"type": "Point",
"coordinates": [2.3645320381923534, 48.816341825787724]
,
"properties":
"event_type": "imp"
,
"type": "Feature",
"geometry":
"type": "Point",
"coordinates": [2.3274838513968072, 48.86982967859056]
,
"properties":
"event_type": "imp"
,
"type": "Feature",
"geometry":
"type": "Point",
"coordinates": [2.23284629237154, 48.89111120713999]
,
"properties":
"event_type": "imp"
,
"type": "Feature",
"geometry":
"type": "Point",
"coordinates": [2.23284629237154, 48.89111120713999]
,
"properties":
"event_type": "click"
,
"type": "Feature",
"geometry":
"type": "Point",
"coordinates": [2.4204737962258305, 48.85038737901075]
,
"properties":
"event_type": "imp"
,
"type": "Feature",
"geometry":
"type": "Point",
"coordinates": [2.4214870126092594, 48.86339618816508]
,
"properties":
"event_type": "imp"
,
"type": "Feature",
"geometry":
"type": "Point",
"coordinates": [2.4144620076022436, 48.876530301936576]
,
"properties":
"event_type": "imp"
,
"type": "Feature",
"geometry":
"type": "Point",
"coordinates": [2.2470618097659285, 48.88845096504584]
,
"properties":
"event_type": "imp"
]
模型
这是使用 mongoose 的架构(如您所见,我确实添加了 2dsphere 索引):
const mongoose = require("mongoose");
const FeatureSchema = new mongoose.Schema(
type: String,
geometry:
type: type: String, default: "Point" ,
coordinates: type: [Number]
,
properties: event_type: String
);
FeatureSchema.index( geometry: "2dsphere" );
const MapEventSchema = new mongoose.Schema(
type: String,
features: [FeatureSchema]
);
const MapEvent = mongoose.model("mapEvent", MapEventSchema);
module.exports = MapEvent;
测试
现在这是我正在努力进行的测试(使用 Mocha):
const assert = require("assert");
const MapEvent = require("../src/models/mapEvents");
const fs = require("fs");
describe("Read test", () =>
// create new room collection because collection is drop between each file
beforeEach(done =>
rawJSON = fs.readFileSync("./src/assets/test_assets/read_test.json");
const parsedContent = JSON.parse(rawJSON);
const mapEvent = new MapEvent(parsedContent);
mapEvent
.save()
.then(() =>
done();
)
.catch(error =>
console.error(error);
);
);
it("Find nearest event by coordonates", done =>
const distance = 1000;
const lat = 48.86;
const lon = 2.35;
function waitForIndex()
return new Promise((resolve, reject) =>
MapEvent.on("index", error => (error ? reject(error) : resolve()));
);
MapEvent.findOne(
geometry:
$near: [lat, lon],
$maxDistance: distance
)
//.then(waitForIndex)
.then(MapEvent.init())
.then(nearestResult =>
console.log("------------");
console.log(nearestResult);
console.log("------E-----");
assert(nearestResult);
done();
)
.catch(error =>
console.error("************");
console.error(error);
console.error("******E*****");
);
);
);
输出
您可能会注意到,我注释掉了 waitForIndex()(在相关的 *** 问题上找到)改为使用 MapEvent.init()(在 github 上找到)。不幸的是,两种解决方案都给了我相同的错误输出。
Connection is established
Create test
(node:12483) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.
✓ MapEvent saving
Read test
************
MongoError: error processing query: ns=babylon_ad_db.mapevents batchSize=1 limit=1Tree: GEONEAR field=geometry maxdist=1000 isNearSphere=0
Sort:
Proj:
planner returned error: unable to find index for $geoNear query
at queryCallback (/home/geoffrey/babylon-ad/node_modules/mongodb-core/lib/cursor.js:248:25)
at /home/geoffrey/babylon-ad/node_modules/mongodb-core/lib/connection/pool.js:532:18
at process._tickCallback (internal/process/next_tick.js:61:11)
ok: 0,
errmsg:
'error processing query: ns=babylon_ad_db.mapevents batchSize=1 limit=1Tree: GEONEAR field=geometry maxdist=1000 isNearSphere=0\nSort: \nProj: \n planner returned error: unable to find index for $geoNear query',
code: 2,
codeName: 'BadValue',
operationTime:
Timestamp _bsontype: 'Timestamp', low_: 7, high_: 1536667477 ,
'$clusterTime':
clusterTime:
Timestamp _bsontype: 'Timestamp', low_: 7, high_: 1536667477 ,
signature: hash: [Binary], keyId: [Long] ,
name: 'MongoError',
[Symbol(mongoErrorContextSymbol)]:
******E*****
1) Find nearest event by coordonates
1 passing (3s)
1 failing
1) Read test
Find nearest event by coordonates:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/home/geoffrey/babylon-ad/test/read_test.js)
我已经做了一些谷歌搜索,这就是我听说 $near 和 2dsphere 索引的方式。但是,我仍然无法弄清楚为什么它说它找不到索引。即使我使用 waitForIndex() 或 MapEvent.init() 等待它。
这个社区创造了一些很酷的奇迹,希望你能帮助我。
谢谢,
【问题讨论】:
没人知道吗? 【参考方案1】:此类错误表示未创建索引。这通常是由于坐标数据无效。也许加载你的 mongo shell 并确保你的索引存在:
mongo
use yourdatabasename
db.yourcollection.getIndexes()
你应该得到类似的结果:
"v" : 2,
"key" :
"geometry" : "2dsphere"
,
"name" : "geometry_2dsphere",
"ns" : "databasename.yourcollection",
"2dsphereIndexVersion" : 3
如果不存在,请尝试在 shell 中创建索引 - 如果无法创建索引,则会引发错误。:
db.yourcollection.createIndex( geometry : "2dsphere" )
【讨论】:
可能是创建了索引,但使用了错误的说明符,例如在我的例子中,我为coordinates
创建了一个2dsphere
索引而不是location
,它适用于某些查询,但不适用于$geoNear
。
是的@hlongmore 那是我的错误。我做了db.restaurants.ensureIndex(zipcode:"2dsphere")
,而我应该做db.restaurants.ensureIndex(location:"2dsphere")
,后者解决了我的问题!以上是关于$near 错误 - 规划器返回错误:无法找到 $geoNear 查询的索引的主要内容,如果未能解决你的问题,请参考以下文章
ebaySOAP 示例示例给出错误 - 无法找到包装器“https”
Gradle(包装器)+ STS + Spring Boot:错误:无法找到或加载主类 1.1,
centos7系统出现“-bash:syntax error near unexpected token `('”错误的解决方法
NestJS + Angular 项目抛出未找到模块:错误:无法解析“缓存管理器”