title: Mongoose near(...) query on 2dsphere indexed field not returning valid results
body: <p>I am unable to query a mongodb database using mongoose's <code>qry.where().near()</code> syntax.</p>
  
  <p>I have a Schema with coordinates stored as an array, indexed as <code>2dsphere</code>:</p>
  
  <pre><code>loc : { type: [Number], index: '2dsphere' }
  </code></pre>
  
  <p>I am running a mongoose query using <code>.where().near()</code>:</p>
  
  <pre><code>qry.where('loc').near({
      center: search.loc,
      maxDistance: search.distance * 1000
  });
  </code></pre>
  
  <p>I have enabled <code>mongoose.set('debug', true)</code>, and can see the resulting debug:</p>
  
  <h3>Inserting 2 documents in the collection with coordinates <code>[ 10, -20 ]</code>:</h3>
  
  <pre><code>Mongoose: models.insert({ _id: ..., loc: [ 10, -20 ], type: 'apple' }) {}  
  Mongoose: models.insert({ _id: ..., loc: [ 10, -20 ], type: 'orange' }) {}  
  </code></pre>
  
  <h3>Searching for documents near coordinates <code>[ 10, -20 ]</code>:</h3>
  
  <pre><code>Mongoose: models.find({ loc: { '$near': [ 10, -20 ], '$maxDistance': 1000 } })
  </code></pre>
  
  <p><em>The query returns <strong>no results.</em></strong></p>
  
  <p>I have proved the documents are in the database by searching on <code>type</code>. Its the geospatial query I can't get to work.</p>
  
  <p>I am using <code>mongodb v2.6.1</code> and <code>mongoose v3.8.8</code></p>
  
  <h2>Full working example below:</h2>
  
  <h3>Schema: (model.js)</h3>
  
  <pre><code>var mongoose = require('mongoose'),
      Schema = mongoose.Schema;
  
  var ModelSchema = new Schema({
      type : { type: String, index: true },
      loc  : { type: [Number], index: '2dsphere' }
  });
  
  ModelSchema.index({type: 1, loc: 1});
  
  ModelSchema.statics.search = function(search, cb) {
      var qry = this.find();
      if (search.types) {
          qry.where('type').in(search.types);
      }
      if (search.loc) {
          qry.where('loc').near({
              center: search.loc,
              maxDistance: search.distance * 1000
          });
      }
      qry.exec(cb);
  };
  </code></pre>
  
  <h3>Mocha test: (test.ts)</h3>
  
  <pre><code>var q = require('node-promise'),
      should = require('should'),
      mongoose = require('mongoose');
  
  mongoose.connect('mongodb://localhost/my-test');
  var Model = require('./model');
  
  describe('My Model', function() {
      before(function(done) {
          mongoose.set('debug', true);
  
          Model.remove().exec(function() {
              var create = function(promises, body) {
                  var p = new q.Promise();
                  var m = new Model(body);
                  m.save(function(err) {
                      if (err) {
                          console.log(err);
                          p.reject();
                      } else {
                          p.resolve();
                      }
                  });
                  promises.push(p);
              };
  
              var promises = [];
              create(promises, { type: 'apple',  loc: [ 10, -20 ] });
              create(promises, { type: 'orange', loc: [ 10, -20 ] });
              create(promises, { type: 'apple',  loc: [ 15,  10 ] });
              create(promises, { type: 'orange', loc: [ 15,  10 ] });
              q.all(promises).then(function() {
                  done();
              })
          });
      });
  
      it('should find all', function(done) {
          Model.search({}, function(err, items) {
              items.should.have.length(4);
              done();
          });
      });
  
      it('should find apples', function(done) {
          Model.search({types: ['apple']}, function(err, items) {
              items.should.have.length(2);
              done();
          });
      });
  
      // this test fails
      it('should find coords', function(done) {
          Model.search({loc: [ 10, -20 ], distance: 1}, function(err, items) {
              items.should.have.length(2);
              done();
          });
      });
  
  });
  </code></pre>
  
  <h3>package.json:</h3>
  
  <pre><code>{
    "name": "test",
    "version": "0.0.0",
    "dependencies": {
      "mongoose": "~3.8.8"
    },
    "devDependencies": {
      "should": "~2.1.0",
      "node-promise": "0.5.10"
    }
  }
  </code></pre>
  
  <h3>Run through mocha:</h3>
  
  <pre><code>sudo npm install -g mocha
  npm install
  mocha ./test.js
  </code></pre>