开发者

Geo spatial index in mongodb with node.js

开发者 https://www.devze.com 2023-04-03 17:38 出处:网络
I am finding problem in defining the geo spatial index \'2d\' as shown below. Any clue as to what is going wrong ?

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");
                    }
              开发者_StackOverflow社区      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)
            })


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消