简体   繁体   中英

Setting mongoose geospatial index range

I am trying to implement a tilemap using mongodb geospatial indexes. I run a server script which includes the following piece of code:

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

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

But no matter what value I set for 'min' and 'max' properties it still throws error when I try to save a Tile which is eg out of 180 longitude. Is this even the correct way to set these properties in mongoose?

The following does not throw any error.

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

A debug log where I tried to assign max and min to 500 and -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' }

The find queries in the beginning can be ignored I guess, they are just effect of my scrolling on the map in the game client.

这是由于我每次运行该应用程序时不小心重新创建了索引所导致的,因为我没有意识到我必须在执行此操作之前每次都删除它们。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM