Skip to content

Instantly share code, notes, and snippets.

@aheckmann
Created March 25, 2013 22:50
Show Gist options
  • Star 38 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save aheckmann/5241574 to your computer and use it in GitHub Desktop.
Save aheckmann/5241574 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();
});
}
@SarasArya
Copy link

Without this. I could have never done it.

@houhashv
Copy link

great job. helped me a lot!

@alessiodionisi
Copy link

Thank you!

@PabloEzequiel
Copy link

thanks! It was ok

@janschmutz
Copy link

Thanks a lot, I wish I found this earlier,there is a lot of misinformation about this issue out there. This worked perfectly

@assimelha
Copy link

It's 2017 and this is still useful. Awesome !

@emilioriosvz
Copy link

It's 2018 and this is still useful. Thanks.

@AdrianSaliaj
Copy link

AdrianSaliaj commented Jan 22, 2019

It's 2019 and this is still useful. Nice.

@aaoafk
Copy link

aaoafk commented Jun 26, 2019

Its 2019 and this is still painful but thanks!

@karanp41
Copy link

karanp41 commented Mar 9, 2021

Its 2021 now and yet the best!

@asadsohail-x
Copy link

This is 2022 and this just saved my job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment