我已经在mongo / mongoose文档中度过了一天,并且在堆栈溢出时似乎无法解决这一问题。
这是我的基本架构设置:
const mongoose = require('mongoose');
const config = require('./config/config');
const db = mongoose.createConnection(config.uri, { autoIndex: false });
const storeSchema = mongoose.Schema({
name: String,
location: { type:[Number], index:'2dsphere', required:true },
});
const Store = db.model('Store', storeSchema);
我也尝试过:
const storeSchema = mongoose.Schema({
name: String,
location: { type: { type: String }, coordinates: [Number] },
});
storeSchema.index({ location: "2dsphere" });
我在createConnection上将autoIndex设置为false,因为在应用程序启动时Mongoose automatically calls createIndex,并且$ geoNear查询要求只有一个索引。我以为Mongoose可能正在创建重复索引,但这并不能解决问题。
我创建了这样的商店记录(简化):
const coordinates = { lng: -122.0266515, lat: 36.9743292 }
Store.create({
name: "My Store",
location: [coordinates.lng, coordinates.lat],
})
这是我的查询,返回错误:
const location = { longitude: -122.026423, latitude: 36.974538 }
// above coordinates are near the 'My Store' record's coordinates.
const point = {
type: "Point",
coordinates: [location.longitude, location.latitude]
}
Store.aggregate([{
$geoNear: {
near: point,
distanceField: "dist.calculated",
maxDistance: 100000,
spherical: true
}
}
])
.then((results) => console.log(results))
.catch((error) => console.log(error));
这是我收到的“ geoNear没有地理索引” 错误:
{ MongoError: geoNear command failed: { ok: 0.0, errmsg: "no geo indices
for geoNear", operationTime: Timestamp(1529989103, 8), $clusterTime: {
clusterTime: Timestamp(1529989103, 8), signature: { hash: BinData(0,
0000000000000000000000000000000000000000), keyId: 0 } } }
at queryCallback (/Users/`...`/node_modules/mongodb-
core/lib/cursor.js:244:25)
at /Users/`...`/node_modules/mongodb-
core/lib/connection/pool.js:544:18
at process._tickCallback (internal/process/next_tick.js:150:11)
name: 'MongoError',
message: 'geoNear command failed: { ok: 0.0, errmsg: "no geo indices for
geoNear", operationTime: Timestamp(1529989103, 8), $clusterTime: {
clusterTime: Timestamp(1529989103, 8), signature: { hash: BinData(0,
0000000000000000000000000000000000000000), keyId: 0 } } }',
operationTime: Timestamp { _bsontype: 'Timestamp', low_: 8, high_:
1529989103 },
ok: 0,
errmsg: 'geoNear command failed: { ok: 0.0, errmsg: "no geo indices for
geoNear", operationTime: Timestamp(1529989103, 8), $clusterTime: {
clusterTime: Timestamp(1529989103, 8), signature: { hash: BinData(0,
0000000000000000000000000000000000000000), keyId: 0 } } }',
code: 16604,
codeName: 'Location16604',
'$clusterTime':
{ clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 8, high_:
1529989103 },
`enter code here`signature: { hash: [Binary], keyId: [Long] } } }
当我console.log storeSchema.index()._ indexes 时,我得到以下信息:
[ [ { location: '2dsphere' }, {} ], [ {}, {} ] ]
...因此索引似乎在那里。
我还尝试了在运行查询after seeing the following stack overflow conversation之前调用sureIndexes。
Store.ensureIndexes({location: '2dsphere'})
答案 0 :(得分:0)
好,所以我发现错误实际上与创建数据库实例时MongoDB Atlas提供的URI有关。
具体来说,他们提供的URI(我已复制并粘贴)如下所示:
mongodb+srv://user:password@host.mongodb.net?
readPreference=primaryPreferred&retryWrites=true
删除“ retryWrites = true”对我来说已解决此问题。此处提供更多详细信息: