2013-10-06 77 views
4

我试图通过使用MongoDB的find方法查询某个点周围的纬度和经度点来使用MongoDB的地理空间索引。我不断收到错误:MongoError:找不到任何特殊索引:2d(需要索引),2dsphere(需要索引)

MongoError: can't find any special indices: 2d (needs index), 2dsphere (needs index)

我不知道在哪里的文档谷歌搜索了大约一个小时后,就是这个。我也找不到任何好的解释。下面是我用猫鼬创建的模式:

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); 

我然后使用找到的方法来找到其他事件各地用户提供的点文件。

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; 
    } 
}); 

我在这里有什么语法错误?我错过了一些巨大的东西?除this link之外,任何文档的任何URL都会很好。

+0

你真的做了索引吗? http://docs.mongodb.org/manual/core/geospatial-indexes/ quig搜索mongodb 2d索引 – Sammaye

回答

2

您是否尝试用这句话创建索引?

db.event.ensureIndex({ "location" : "2d" })

这是有点令人困惑的例子。如果'location'是你的坐标或'geo',我不会得到,因为你用前者创建了模式,而用后者进行查询。

在这两种情况下,一个好主意是执行

db.event.findOne().pretty()

所以,你可以查看你的收藏的格式。

也可以使用此命令检查当前的索引。

db.event.getIndexes()

所有这些工作,如果你正在'事件'集合。