0

I am trying to load a document in Mongoose if it's let's say 10 kilometers from a current lat/lon coordinate.

So let's say I have this object:

{
    lat: -33.9493992,
    lon: 151.1114885
}

And in my Mongoose database I have a collection and every document within this collection also has the save lat-lon keys. How can I only grab entries of a certain distance from a given coordinate?

So for example I want to grab only the documents that are up to 10 kilometers distant from the above coordinate.

I would appreciate any help.

EDIT: My code:

Initializing the model:

var schema = new mongoose.Schema({
title: String,
    title: String,
    coordinates: {
        type: [ Number ],
        default: [0, 0],
        index: '2dsphere'
    }
});

global.models.Point = mongoose.model('Point', schema);

And the code trying to find in the model: router.route('/points/:lat/:lon') .get(function (req, res) { console.log('lat: ' + req.params.lat + ' lon:' + req.params.lon);

        global.models.Point.find({
            coordinates: {
                $near: {
                    $maxDistance: 100 / 111.12,
                    $geometry: {
                        type: 'Point',
                        coordinates: [req.params.lon, req.params.lat]
                    }
                }
            }
        }, function (err, res) {
            if (err) {
                return console.log(err);
            }
            console.log(res);
        });
    }); 

The object as it is stored in the database collection:

{ 
    "_id" : ObjectId("56ea103eefedec96b6d4b203"), 
    "title" : "Some title", 
    "coordinates" : [
        5.2260167, 
        52.3500607
    ]
}
Ariel Weinberger
  • 561
  • 1
  • 6
  • 12
  • 1
    Read the documentation. [`$nearSphere`](https://docs.mongodb.org/manual/reference/operator/query/nearSphere/). You don't want seperate properties. The documentation will tell you how your document is supposed to store the data. – Blakes Seven Mar 29 '16 at 13:27

1 Answers1

0

In your schema you need to have a field defined as following:

location: { 
  type: [ Number ], 
  default: [ 0, 0 ], 
  index: '2dsphere' 
}

Then you can do search operations on it like that:

Model.find({
  location: {
    $near: {
      $maxDistance: 100/111.12, // 100km
      $geometry: { 
        type: "Point", 
        coordinates: [151.1114885, -33.9493992] // [lon, lat]    
      }
    }
  }
}).exec(callback);

Explanation note for $maxDistance:

1° latitude = 69.047 miles = 111.12 kilometers

oleh.meleshko
  • 4,675
  • 2
  • 27
  • 40
  • Hey thanks for the answer. Do I need to define the fields defined when I initiate the model, using mongoose modelSchema? I did that and I get "undefined" as the result... – Ariel Weinberger Mar 29 '16 at 13:39
  • I get an empty object, there must be something wrong in my Schema. I will update my question with the schema definition. I would appreciate help. Thanks a lot Oleg. – Ariel Weinberger Mar 29 '16 at 13:55
  • @ArielWeinberger coordinates should be an array field, not an object. Please check my answer one more time – oleh.meleshko Mar 29 '16 at 14:16
  • Hey. Actually I forgot to edit, but I did use it as an array like you did, yet I still get an empty array as a result :( – Ariel Weinberger Mar 29 '16 at 14:44
  • I also edited and added the document as it is stored in the collection, maybe I did something wrong there... – Ariel Weinberger Mar 29 '16 at 15:11