10

I am finding problem in defining the geo spatial index '2d' as shown below. Any clue as to what is going wrong ?

var Address = new Schema({
      loc           : {lat: Number,  lng: Number },
      Address       : String,
      create_date       : {type: Date, default: Date.now}
});
Address.index ({
       loc : "2d"
});

It throws error like,

events.js:45 throw arguments[1]; // Unhandled 'error' event ^ Error: point not in range at [object Object]. (/cygdrive/c/Personal/software/ nodejs/NODE/no de_modules/mongoose/node_modules/mongodb/lib/mongodb/db.js:503:20)

EDIT: added the code

var Address = new Schema({
      type              : {type: String, enum: ['Apartment', 'House', 'Serviced Apartment'], default: 'Apartment'}, 
      loc               : {lat: Number,  lng: Number },
      Address           : String,
      create_date       : {type: Date, default: Date.now}
});

/*
Address.index ({
    loc : "2d"
});
*/

mongoose.connect('mongodb://127.0.0.1:27017/test123', function(err) {
    if (err) {
        console.log("error in mongo connection");
        throw err;
    }
    console.log("connected to mongo");
});

var RentModel = mongoose.model('Rent', Address);



socket = io.listen(app);

socket.sockets.on('connection', function(client){ 

        console.log('inside on connection');

        client.on('register', function(msg){ 
                console.log("msg.geometry.type", msg.geometry.type);

                var rent = new RentModel();
                rent.type = 'Apartment';
                rent.loc.lat = 23;
                rent.loc.lng = 56;
                rent.Address = "LLLLLLLLIIIIIIIOOOOOOONNNNNNNN"

                console.log("before save");
                rent.save(function(err){
                    console.log("rent.save start");
                    if(err) { 
                        throw err; 
                        console.log("error in save");
                    }
                    console.log("saved");

                });

            }); 


            RentModel.find({loc : { $near : [20, 50], $maxDistance: 30 }} , function(err, docs){
                if (err) {
                    console.log("error in finding near", err);
                    throw err;
                }
                console.log('docs.length : ' , docs.length);
                console.log('docs : ',docs)
            })
3
  • Looks like you're also using Mongoose. Do you have an actual object/query that you're performing? The problem could be in the Node driver, it could be in Mongoose or it could be in the code, so we need just a little more to go on. Commented Sep 8, 2011 at 16:29
  • Yes, you are right. I am using mongoose. I have edited the question above and added the code. save is happening properly. When i do a find it complains about index and when I add index, it says, "point not in range" Commented Sep 8, 2011 at 16:44
  • 1
    Well, I changed the attribute name from loc to "location" and changed "2d" to '2d' and it seems, it's working now. This is strange. Commented Sep 9, 2011 at 3:53

2 Answers 2

9

It's also worth noting that you will want longitude to come before latitude in your array. This will not affect you when you are using 2D, but it will when you are using 3D. Mathematically this makes sense as longitude is the X coordinate and latitude is the Y coordinate (x,y), but most of us are familiar with lat coming before long (and one of the best Mongo books out there has an example with lat before long, but it does not cover 2D).

Ultimately you're likely going to want to use 3D as 2D calculations are not accurate as you move away from the equator.


UPDATE: 2015-12-08 The above is no longer relevant - please see updated answers/API docs

Sign up to request clarification or add additional context in comments.

3 Comments

glad you found it useful :) By the way, there are two great interactive tutorials here (including one dedicated to geo): mongly.com
great slide show from Greg who does the GeoSpatial stuff at Mongo ..Some of this could be outdated by now - I haven't been working with Mongo in a few months, but I thought this was worth sharing: bit.ly/GIGevE
For anybody coming across this now - there is now a '2dsphere' index which uses spherical coordinates rather than cartesian.
0

You may want some reference here on how to do it :) And for someone who comes after. Just in case, someone is interested

http://www.jmanzano.es/blog/?p=592

1 Comment

Thank you Javier. This was solved long back.. Anyway, I do it as someSchema.index({ g_location : '2d' });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.