rahulroy9202
rahulroy9202

Reputation: 2848

GeoNear with Mongoose

How do I query the model for geoNear with maxDistance of 1km?

This is my mongoose schema -

var userDestinationSchema = mongoose.Schema({

    uid: String,                
    update_time: Date,          

    location:{                  //<INDEXED as 2d>
        lon: Number,
        lat: Number
    }
});



var userDestinationModel = mongoose.model('userDestinationModel', userDestinationSchema);

I tried doing this and it ain't working.

userDestinationModel.geoNear([parseInt(req.params.lon,10),parseInt(req.params.lat,10)],
              { maxDistance : 1, spherical : true }, 
              function(err, results, stats) {
                   console.log(results);
                });


but when I remove spherical : true it works.

What is the unit of maxDistance? //Answer: Radian in case of Legacy Coordinate.

DataSample:

{ _id: ObjectId("52f89d8cdbe6f9ea80344fd9"), location: { lon: 165, lat: 165 }, uid: "testuser2", update_time: ISODate("2014-02-10T09:36:12.705Z") }
{ _id: ObjectId("52f89d97dbe6f9ea80344fda"), location: { lon: 166, lat: 166 }, uid: "testuser3", update_time: ISODate("2014-02-10T09:36:23.236Z") }
{ _id: ObjectId("52f8a16edbe6f9ea80344fdb"), location: { lon: 164, lat: 164 }, uid: "testuser4", update_time: ISODate("2014-02-10T09:52:46.464Z") }
{ _id: ObjectId("52f8a173dbe6f9ea80344fdc"), location: { lon: 164, lat: 164 }, uid: "testuser1", update_time: ISODate("2014-02-10T09:52:51.125Z") }
{ _id: ObjectId("52f8a17fdbe6f9ea80344fdd"), location: { lon: 165, lat: 165 }, uid: "testuser7", update_time: ISODate("2014-02-10T09:53:03.365Z") }

Upvotes: 1

Views: 2825

Answers (1)

DhruvPathak
DhruvPathak

Reputation: 43275

The unit is metres, so you might not be getting correct answer. For km specify 50*1000

And perhaps you need to edit your querying document according to this:

http://docs.mongodb.org/manual/reference/command/geoNear/

{ near: POINT_COORDINATES.....

Upvotes: 2

Related Questions