//test.js
var mongoose = require('mongoose');
mongoose.connect('localhost','geojsonTest');
var Schema = mongoose.Schema;
//--------------------------------------------------------------------
var places = new Schema({
'geom': {
'type' : { type: String, default: "Point" },
'coordinates': [{type: "Number"}]
},
'pointType': String,
'name': String,
'comment': String
});
//set a geoHaystack index for optimised small-area searches
//placesSchema.index({'coordinates' : 'geoHaystack', 'pointType' : 1},{bucketSize : 1}); // also results in same error with geoSearch
places.index({'coordinates' : '2dsphere'});
var Places = mongoose.model('Places', places);
// define some dummy data
var data = [
{ pointType: 'Place', name: 'Little Snoring', comment: 'Bogus, Dude!', geom: { type: 'Point', coordinates: [-20.0, 5.0] }},
{ pointType: 'Place', name: 'Fantasia Hollow', comment: 'Awesome!', geom: { type: 'Point', coordinates: [6.0, 10.0] }},
{ pointType: 'Place', name: "Lion's Den", comment: 'Growl-tastic', geom: { type: 'Point', coordinates: [34.0, -50.0] }},
{ pointType: 'Place', name: 'Dun Roamin', comment: 'So unoriginal', geom: { type: 'Point', coordinates: [-100.0, 70.0] }},
{ pointType: 'Sausage', name: 'Floodsville', comment: 'No way (sad face)', geom: { type: 'Point', coordinates: [38.0, 38.0] }}
];
data.forEach(function(item){
console.log('Loading item: ' + item['name']);
Places.create(item, function (err, small) {
if (err) return handleError(err);
});
});
var testPoint = { type : "Point", coordinates : [-20.0, 5.0] };
Places.geoNear(testPoint, { maxDistance : 25, spherical : true }, function (err, results, stats) {
if (err) throw err;
console.log('Result: ' + results);
});