2014-08-30 54 views
0

我正在嘗試使用mongodb地理空間索引實現地圖。 我運行一個服務器腳本,其中包括下面的一段代碼:設置貓鼬地理空間索引範圍

var TileSchema = mongoose.Schema({ 
    location: [Number], 
    tile_type: Number 
}); 

TileSchema.index({location: '2d'}, {min: 500, max: 500}); 

但不管我了「分鐘」和「最大」屬性設置什麼樣的價值,當我嘗試保存瓷磚這是它仍然拋出錯誤例如在180經度。這甚至是用貓鼬設置這些屬性的正確方法嗎?

以下不會引發任何錯誤。

Tile.on('index', function (err) { 
    if (err) console.error(err); // error occurred during index creation 
}) 

我就開始想分配最大分鐘調試日誌 500和-500:

Listening on port 1337... 
Mongoose: tiles.ensureIndex({ location: '2d' }) { safe: undefined, background: true, max: 500, min: -500 } 
Mongoose: tiles.find({ location: { '$geoWithin': { '$box': [ [ -48, -48 ], [ 48, 48 ] ] } } }) { fields: undefined } 
Mongoose: tiles.find({ location: { '$geoWithin': { '$box': [ [ -48, -48 ], [ 48, 48 ] ] } } }) { fields: undefined } 
Mongoose: tiles.find({ location: { '$geoWithin': { '$box': [ [ -64, -48 ], [ 32, 32 ] ] } } }) { fields: undefined } 
Mongoose: tiles.find({ location: { '$geoWithin': { '$box': [ [ -112, -48 ], [ -32, 32 ] ] } } }) { fields: undefined } 
Mongoose: tiles.find({ location: { '$geoWithin': { '$box': [ [ -176, -48 ], [ -96, 32 ] ] } } }) { fields: undefined } 
Mongoose: tiles.find({ location: { '$geoWithin': { '$box': [ [ -240, -64 ], [ -160, 32 ] ] } } }) { fields: undefined } 
Mongoose: tiles.find({ location: { '$geoWithin': { '$box': [ [ -272, -64 ], [ -192, 16 ] ] } } }) { fields: undefined } 
{ [MongoError: point not in interval of [ -180, 180 ] :: caused by :: { 0: -186.0, 1: -24.0 }] name: 'MongoError' } 
Mongoose: tiles.insert({ __v: 0, _id: ObjectId("54034534c480de3512a82523"), tile_type: 100, location: [ -219, -27 ] }) {} 
{ [MongoError: insertDocument :: caused by :: 16755 Can't extract geo keys from object, malformed geometry?: { _id: ObjectId('54034534c480de3512a82523'), location: [ -219, -27 ], tile_type: 100, __v: 0 }] 
    name: 'MongoError', 
    code: 16755, 
    err: 'insertDocument :: caused by :: 16755 Can\'t extract geo keys from object, malformed geometry?: { _id: ObjectId(\'54034534c480de3512a82523\'), location: [ -219, -27 ], tile_type: 100, __v: 0 }' } 
Mongoose: tiles.find({ location: { '$geoWithin': { '$box': [ [ -288, -64 ], [ -208, 16 ] ] } } }) { fields: undefined } 
{ [MongoError: point not in interval of [ -180, 180 ] :: caused by :: { 0: -194.0, 1: -24.0 }] name: 'MongoError' } 
Mongoose: tiles.find({ location: { '$geoWithin': { '$box': [ [ -272, -64 ], [ -192, 16 ] ] } } }) { fields: undefined } 
{ [MongoError: point not in interval of [ -180, 180 ] :: caused by :: { 0: -186.0, 1: -24.0 }] name: 'MongoError' } 
Mongoose: tiles.find({ location: { '$geoWithin': { '$box': [ [ -272, -64 ], [ -192, 16 ] ] } } }) { fields: undefined } 
{ [MongoError: point not in interval of [ -180, 180 ] :: caused by :: { 0: -186.0, 1: -24.0 }] name: 'MongoError' } 

發現在開始查詢可以忽略我猜,他們只是我在遊戲客戶端的地圖上滾動的效果。

+1

我不完全理解。你爲什麼設定最小和最大?向我們展示導致錯誤和錯誤的插入。打開貓鼬調試,並告訴我們對索引操作和插入說什麼。 – wdberkeley 2014-08-31 15:04:35

+0

我更新了問題。我希望現在更清楚。我試圖設置最小和最大,因爲我想實現類似於這個http://blog.nodeknockout.com/post/35215504793/the-wonderful-world-of-geospatial-indexes-in-mongodb – 2014-08-31 16:01:01

+0

工作正常對於我來說,當我測試添加2d索引時,插入由於格式不正確而出錯的文檔,然後使用給定的地理查詢進行查找。嘗試在mongo shell中的玩具集合中進行測試,看看它是否適用於您。這是什麼版本的MongoDB?瓷磚集合上還有哪些其他指標? – wdberkeley 2014-08-31 20:52:53

回答

0

這是由於我每次運行應用程序時意外重新創建了索引,因爲我沒有意識到我必須在每次執行此操作前刪除它們。