Node.js 使用MongoDB+;猫鼬
我花了一天时间在mongo/mongoose文档和堆栈溢出上,似乎无法理解这一点 这是我的基本架构设置:Node.js 使用MongoDB+;猫鼬,node.js,mongodb,mongoose,geospatial,geocoding,Node.js,Mongodb,Mongoose,Geospatial,Geocoding,我花了一天时间在mongo/mongoose文档和堆栈溢出上,似乎无法理解这一点 这是我的基本架构设置: const mongoose = require('mongoose'); const config = require('./config/config'); const db = mongoose.createConnection(config.uri, { autoIndex: false }); const storeSchema = mongoose.Schema({ nam
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 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));
我也尝试过:
const storeSchema = mongoose.Schema({
name: String,
location: { type: { type: String }, coordinates: [Number] },
});
storeSchema.index({ location: "2dsphere" });
我在createConnection上将autoIndex设置为false,因为在app start和$geoNear查询中,只需要一个索引。我以为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 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));
这是我返回错误的查询:
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 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()时,会得到以下索引:
[ [ { location: '2dsphere' }, {} ], [ {}, {} ] ]
…所以索引似乎就在那里
在运行查询之前,我还尝试调用EnsureIndex
好的,我发现这个错误实际上与我创建db实例时MongoDB Atlas提供的URI有关 具体地说,它们提供的URI(我已经复制并粘贴)如下所示:
mongodb+srv://user:password@host.mongodb.net?
readPreference=primaryPreferred&retryWrites=true
删除“retryWrites=true”为我解决了这个问题。更多详细信息请参见: