title: Mongoose Mongo 2dsphere geoWithin
body: <p>After reading many questions that are SO close to mine, and reading the MongoDB docs and Mongoose docs, I still cannot answer my question.  </p>
  
  <p>Using express 4.13.4, mongoose 4.4.10, mongodb 2.1.14 on Node 4.4.0</p>
  
  <p>My Mongoose Location schema:</p>
  
  <pre><code>var schema = new Schema({
    type: {type: String},
    coordinates: []
  },{_id:false});
  
  var model = mongoose.model('LocationModel',schema);
  
  module.exports = {
     model :  model,
     schema : schema
  };
  </code></pre>
  
  <p>My <code>CatalogModel</code> schema (what I write to Mongo):</p>
  
  <pre><code>var locationSchema = require('./locationModel').schema;
  var schema = new Schema({
      title : String, 
      format: {type: String, maxlength: 4},
      location: {type: locationSchema, required:true},
      otherStuff: String
  });
  schema.index({location: '2dsphere'}); // Ensures 2dsphere index for location
  
  model = mongoose.model('CatalogModel',schema);
  </code></pre>
  
  <p>I create a concrete example and write to MongoDB (this works fine... in that I can query it in Mongo)</p>
  
  <pre><code>var polyEntry = new CatalogModel({
      title:"I am just a Polygon",
      otherStuff: "More stuff here",
      location:{
          type:'Polygon',
          coordinates:[[[0,1],[0,2],[1,2],[0,1]]]
      }
  });
  </code></pre>
  
  <p>In Mongo, I asked the collection for the indexes:</p>
  
  <pre><code>db.catalogmodels.getIndexes()
  </code></pre>
  
  <p>And this is what it says (not entirely sure what this means)</p>
  
  <pre><code>[
      {
          "v" : 1,
          "key" : {
              "_id" : 1
          },
          "name" : "_id_",
          "ns" : "test.catalogmodels"
      },
      {
          "v" : 1,
          "key" : {
              "location" : "2dsphere"
          },
          "name" : "location_2dsphere",
          "ns" : "test.catalogmodels",
          "background" : true,
          "2dsphereIndexVersion" : 3
      }
  ]
  </code></pre>
  
  <p>I can do a <code>db.catalogmodels.find()</code> and get my document back.  </p>
  
  <pre><code>{ 
      "_id" : ObjectId("12345678901234566778"), 
      "title" : "I am just a Polygon", 
      "location" : { 
          "type" : "Polygon", 
          "coordinates" : [ [ [ 0, 1 ], [ 0, 2 ], [ 1, 2 ], [ 0, 1 ] ] ] 
      },        
      "__v" : 0 
  }
  </code></pre>
  
  <p>I can even do a $geoWithin call in Mongo:</p>
  
  <pre><code>db.catalogmodels.find(
      {
          location:{
              $geoWithin:{
                  $geometry:{
                      type:"Polygon",
                      "coordinates":[[[-1,0],[-1,3],[4,3],[4,0],[-1,0]]]
                  }
              }
          }
      })
  </code></pre>
  
  <p><strong>But here's the actual question</strong>:  </p>
  
  <p>Mongoose keeps telling me <strong><em>[Error: Can't use $geoWithin]</em></strong></p>
  
  <pre><code>var geoJson = { 
      "type" : "Polygon", 
      "coordinates" : [[[-1,0],[-1,3],[4,3],[4,0],[-1,0]]] 
  };
  CatalogModel
  .find()
  .where('location').within(geoJson)
  .exec(function(err,data){
      if ( err ) { console.log(err); }
      else {console.log("Data: " + data);}
      db.close()
  });
  </code></pre>
  
  <p>I also replaced the .find().where().within() call to:</p>
  
  <pre><code>CatalogEntryModel.find({
      location:{
          $geoWithin:{
              $geometry:{
                  type:"Polygon",
                  "coordinates":[[[-1,0],[-1,3],[4,3],[4,0],[-1,0]]]
              }
          }
      }
  })
  .exec(function(err,data){
      if ( err ) { console.log(err); }
      else {console.log("Data: " + data);}
      db.close();
  });
  </code></pre>
  
  <p>Is there a reason Mongoose does not like the $geoWithin call?  The latest API says this should work.   </p>