我试图通过使用 MongoDB 的 find 方法查询某个点周围的纬度和经度点来使用 MongoDB 的地理空间索引。我不断收到错误:
MongoError:找不到任何特殊索引:2d(需要索引),2dsphere(需要索引)
在谷歌搜索大约一个小时后,我不确定文档在哪里。我也找不到任何好的解释。这是我使用 Mongoose 创建的 Schema:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var EventSchema = new Schema ({
name: String,
description: String,
location: {type: [String], index: '2dsphere'},
time: Date,
photo: Buffer,
activity: String
});
mongoose.model('Event', EventSchema);
然后我使用find方法在用户提供的点周围查找其他事件文档。
var maxDistance = 0.09;
var lonLat = {$geometry: {type: 'Point', coordinates: [34.09,124.34] }};
Event.find({
'geo': {
$near: lonLat,
$maxDistance: maxDistance
}
}).exec(function(err, events) {
if(err) {
throw err;
} else {
return events;
}
});
我在这里有什么语法错误?我错过了什么巨大的东西吗?除了此链接之外,任何文档的任何 url 也都很棒。