Skip to content

Instantly share code, notes, and snippets.

@zj8487
Forked from aheckmann/geoNear.js
Created July 21, 2014 09:26
Show Gist options
  • Save zj8487/526e2acda85a1a23ae64 to your computer and use it in GitHub Desktop.
Save zj8487/526e2acda85a1a23ae64 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_so-16747852';
console.log('dbname: %s', dbname);
mongoose.connect('localhost', dbname);
mongoose.connection.on('error', function () {
console.error('connection error', arguments);
});
var schema = new Schema({
name: String
, loc: [Number]
});
schema.index({ loc: '2d' });
var A = mongoose.model('A', schema);
mongoose.connection.on('open', function () {
var a = new A({ name: 'so-16747852', loc: [8.75913, 40.27239] });
A.on('index', function (err) {
if (err) console.log('index error', err);
a.save(function (err, a) {
if (err) return done(err);
// executing the command
A.db.db.command({
"geoNear": A.collection.name,
"uniqueDocs":true,
"includeLocs":true,
"near":[
8.759131,
40.272393
],
"spherical":false,
"distanceField":"d",
"maxDistance":0.09692224622030236,
"query":{},
"num":3
}, {dbName: dbname }, function (err, doc) {
console.error('found', doc);
done(err);
});
});
})
});
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