1 // import async to make control flow simplier
2 var async = require('async');
4 // import the rest of the normal stuff
5 var mongoose = require('../../lib');
7 require('./geoJSONSchema.js')();
9 var Location = mongoose.model('Location');
11 // define some dummy data
12 // note: the type can be Point, LineString, or Polygon
14 {loc: {type: 'Point', coordinates: [-20.0, 5.0]}},
15 {loc: {type: 'Point', coordinates: [6.0, 10.0]}},
16 {loc: {type: 'Point', coordinates: [34.0, -50.0]}},
17 {loc: {type: 'Point', coordinates: [-100.0, 70.0]}},
18 {loc: {type: 'Point', coordinates: [38.0, 38.0]}}
22 mongoose.connect('mongodb://localhost/locations', function(err) {
27 Location.on('index', function(err) {
31 // create all of the dummy locations
32 async.each(data, function(item, cb) {
33 Location.create(item, cb);
38 // create the location we want to search for
39 var coords = {type: 'Point', coordinates: [-5, 5]};
41 Location.find({loc: {$near: coords}}).limit(1).exec(function(err, res) {
45 console.log('Closest to %s is %s', JSON.stringify(coords), res);
53 Location.remove(function() {
54 mongoose.disconnect();