2013-10-06 97 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()

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