设置mongoose地理空间索引范围

时间:2014-08-30 19:19:49

标签: node.js mongodb geospatial

我正在尝试使用mongodb地理空间索引实现tilemap。 我运行一个服务器脚本,其中包含以下代码:

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

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

但无论我为'min'和'max'属性设置什么值,当我尝试保存Tile时,它仍然会抛出错误。经度180度。这甚至是在mongoose中设置这些属性的正确方法吗?

以下内容不会引发任何错误。

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

调试日志,我尝试将 max min 分配给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 个答案:

答案 0 :(得分:0)

这是因为我每次运行应用程序时都意外地重新创建了索引,因为我没有意识到每次执行此操作之前都必须删除它们。