Skip to content

Instantly share code, notes, and snippets.

@rokity
Forked from aheckmann/geojsonpoint.js
Created October 3, 2017 15:45
Show Gist options
  • Save rokity/ac1a2d7a69c8b986ba1ff0d77b95b3a4 to your computer and use it in GitHub Desktop.
Save rokity/ac1a2d7a69c8b986ba1ff0d77b95b3a4 to your computer and use it in GitHub Desktop.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var assert = require('assert')
console.log('\n===========');
console.log(' mongoose version: %s', mongoose.version);
console.log('========\n\n');
var dbname = 'testing_geojsonPoint';
mongoose.connect('localhost', dbname);
mongoose.connection.on('error', function () {
console.error('connection error', arguments);
});
// schema
var schema = new Schema({
loc: {
type: { type: String }
, coordinates: []
}
});
schema.index({ loc: '2dsphere' });
var A = mongoose.model('A', schema);
mongoose.connection.on('open', function () {
A.on('index', function (err) {
if (err) return done(err);
A.create({ loc: { type: 'Point', coordinates: [-179.0, 0.0] }}, function (err) {
if (err) return done(err);
A.find({ loc: { $near: { type: 'Point', coordinates:[-179.0, 0.0] }}}, function (err, docs) {
if (err) return done(err);
console.log(docs);
done();
})
})
})
});
function done (err) {
if (err) console.error(err.stack);
mongoose.connection.db.dropDatabase(function () {
mongoose.connection.close();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment