Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Geo spatial index in mongodb with node.js

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)
            })
like image 974
user644745 Avatar asked Sep 08 '11 12:09

user644745


People also ask

What is geospatial index in MongoDB?

MongoDB's geospatial indexing allows you to efficiently execute spatial queries on a collection that contains geospatial shapes and points.

What is indexing with respect to Nodejs and MongoDB?

Overview. Indexes are data structures that support the efficient execution of queries in MongoDB. They contain copies of parts of the data in documents to make queries more efficient. Without indexes, MongoDB must scan every document in a collection to find the documents that match each query.

What is indexing in node JS?

index. js typically handles your app startup, routing and other functions of your application and does require other modules to add functionality. If you're running a website or web app it would also handle become a basic HTTP web server replacing the role of something more traditional like Apache.


1 Answers

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

like image 96
Jordan Avatar answered Oct 26 '22 18:10

Jordan