Skip to content

Issue creating GeoSpatial indexes with altered range #459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 30, 2011

Conversation

antz29
Copy link

@antz29 antz29 commented Dec 27, 2011

Hi there

I seem to be having an issue creating a geo spatial index with an altered range.

Here is my test code:

var     mongo       = require('mongodb'),
        Db          = mongo.Db,
        Connection  = mongo.Connection,
        Server      = mongo.Server;

var db = new Db('verve', new Server("127.0.0.1", 27017, {}));

db.open(function(err, db) {
   db.collection('world', function(err,coll) {
      coll.createIndex( { loc : "2d" } , { min : 0 , max : 500}, function() {
         coll.insert({'loc':[500,500],'name': 'test'},{safe:true},function(err) {
            if (err) console.log(err);
            db.close();
         }); 
      });
   });
});

This will always output:

{ [MongoError: point not in interval of [ -180, 180 )]
  name: 'MongoError',
  err: 'point not in interval of [ -180, 180 )',
  code: 13027,
  n: 0,
  connectionId: 81,
  ok: 1 }

Suggesting that the index wasn't created with the correct range. Calling ensureIndex from the Mongo REPL creates the correct index.

I'm using [email protected] and [email protected] with [email protected]

If I'm doing something obviously wrong could you point it out?

Cheers

John

@antz29
Copy link
Author

antz29 commented Dec 26, 2011

See also this Mongoose issue that is possibly related: Automattic/mongoose#664

@antz29
Copy link
Author

antz29 commented Dec 27, 2011

I have fixed the issue which was in db_command.js. It wasn't using min and max options when it built up the command. This code probably wants tidying up, but all tests pass and it solves the issue.

christkv added a commit that referenced this pull request Dec 30, 2011
Issue creating GeoSpatial indexes with altered range
@christkv christkv merged commit 1881ccc into mongodb:master Dec 30, 2011
@thisissami
Copy link

This seems to still be broken for me. I was having the same issue and while searching for a potential solution, I found this page. I just installed [email protected] in order to fix the problem (was running a much older version), but when I reran my code I got the same "point not in interval of [-180, 180)]" error.

Here's my code:

dbConnector.open(function(err, db) {
  if(err) {
    console.log(err.message);
  } else {
    db.collection('artistLocations',function(err,collection){
        if(err){
            console.log(err.message);
        } else {
        collection.ensureIndex({pos:'2d'},{min:200,max:1400},function(err){
            if(err)
                console.log(err.message);
        cursor = collection.find({});
        //global var
        cursor.count(function(err, total) {
          if(err) {
            console.log(err.message);
          } else {

        cursor.each(function(err, artist) {
          if(err) {
            console.log(err.message);
          } else if(artist != null) {
            collection.findAndModify({'_id':artist._id}, [['_id','asc']],
            {$set : {'pos': [600,600]}},function(err){
                if(err) console.log(err);
                else{
                    count++;
                    if(count == total)
                        process.exit();
                }
            });         
            }
        });
        }});

I get the exact same error as antz29 first did. @antz29 have the errors from running the above code gone away for you?

@christkv
Copy link
Contributor

This test passes for me and does exactly what you are trying to do. I'm running MongoDB 2.0.3. I might be you are running an earlier version without min, max support ?

exports.shouldCorrectlyUseMinMaxForSettingRangeInEnsureIndex = function(test) {
  // Establish connection to db  
  client.createCollection('shouldCorrectlyUseMinMaxForSettingRangeInEnsureIndex', function(err, collection) {
    test.equal(null, err);

    collection.ensureIndex({loc:'2d'}, {min:200, max:1400, safe:true}, function(err, indexName) {
      test.equal(null, err);

      collection.insert({loc:[600, 600]}, {safe:true}, function(err, result) {
        test.equal(null, err);
        test.done();
      });
    });
  });    
}

@thisissami
Copy link

hmmm that's really weird. it seems that the area is in my usage of the variable "pos" instead of "loc". using my same code, if i switch it to say "loc", it works totally fine. i didn't realize that word needed to be exact in the past. i've always used other variables, just the altered ranges have been small (-1 to 1 or something like that) where they haven't gotten out of the standard range.

sami

@thisissami
Copy link

wait nvm, that's not true. I just got code to work with another variable elsewhere using a simple insert (a la your code above).

I guess the only other difference between my code and others is that I was using collection.findAndModify() to set new 2D location variables. Maybe the code for findAndModify isn't working properly? (though it does seem to work fine now that I've switched my "pos:'2d'" to "loc:'2d'")

@christkv
Copy link
Contributor

it could be you had an existing index with no min/max

@thisissami
Copy link

I tried using dropIndex() numerous times and the index just didn't exist. I wonder in retrospect if the issue was that the index simply wasn't created a bunch of times for who-knows-what-reason?

whatever the case, it works now. thanks for responding!

@christkv
Copy link
Contributor

you need to call it with safe:true to ensure the error message is returned

@thisissami
Copy link

yeah i noticed that in your code and updated it in mine as well. thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants