title: mongodb geospatial queries polygon inside polygon
body: <p>I have a collection of geoJSON data in a mongoDB database (v2.6.10
  ).  The data consists of thousands of polygons, mostly squares that represent 500mx500m land claims.  This data is being used on a web app that displays the data on a map.</p>
  
  <p>The web app performance is really slow due to the huge number of polygons.  To improve performance I want to limit the queries to the bounds of the current map window.  We are using the Mapbox API and I am able to get map bounds from the API to be used in a mongodb geo-query.</p>
  
  <p>So far I have been unable to get the mongo queries to cooperate.</p>
  
  <p>Here is a sample record of the geoJSON data that is stored in the DB:</p>
  
  <pre><code>{
    "_id" : ObjectId("56ca94e0336aac299182442f"),
    "type" : "Feature",
    "properties" : {
      "TNRTPCD" : "P",
     ## some fields ommitted for privacy reasons ##
      "TRMNTNDT" : null,
      "RNHCTRS" : 20.3496,
      "TTLTPCD" : "PCX"
    },
    "geometry" : {
      "type" : "Polygon",
      "coordinates" : [
        [
          [
            [
              -121.88883540217063,
              50.97489195799901,
              0
            ],
            [
              -121.88883523174192,
              50.97072525131302,
              0
            ],
            [
              -121.8950854466247,
              50.97072527980969,
              0
            ],
            [
              -121.89508560169767,
              50.97489198254216,
              0
            ],
            [
              -121.88883540217063,
              50.97489195799901,
              0
            ]
          ]
        ]
      ]
    }
  }
  </code></pre>
  
  <p>I have tried the following query and it returns no records and no errors:</p>
  
  <pre><code>db.docs.find(
     {
       'geometry.coordinates': {
         $geoWithin: {
            $geometry: {
               type : "Polygon" ,
               coordinates: [[
               [ -121.26571655273438, 49.56352444783811 ], 
               [ -121.26571655273438, 52.719152198289635 ], 
               [ -120.97492218017578, 52.719152198289635 ], 
               [ -120.9749221801757, 49.56352444783811 ],
               [ -121.26571655273438, 49.56352444783811 ]
                ]]
            }
         }
       }
     }
  )
  </code></pre>
  
  <p>I know that there is data within the defined boundary and yet noting returns.  I have tried playing around with the target column.  I tried switching 'geometry.coordinates' to just coordinates, I tried with quotes and without.  I also messed around with the polygon AOI coordinates.  When the first point is not repeated I get an error.</p>
  
  <p>I have also tried switching $geoWithin for $near, $box and $geoIntersects.  With similar results.</p>
  
  <p>I'm guessing that the issue has to do with the geographical index but that's just a hunch.  I tried to put a 2dsphere index on the geometry column using the following command:</p>
  
  <pre><code>db.docs.ensureIndex( { "geometry" : "2dsphere" } );
  </code></pre>
  
  <p>That fails and gives the following error:</p>
  
  <pre><code>"createdCollectionAutomatically" : false,
      "numIndexesBefore" : 1,
      "ok" : 0,
      "errmsg" : "Can't extract geo keys from object, malformed geometry?:
  </code></pre>
  
  <p>When I try putting an index on just the coordinates it seems to work.  I used this command:</p>
  
  <pre><code>db.docs.ensureIndex( { "coordinates" : "2dsphere" } );
  </code></pre>
  
  <p>And got the following output:</p>
  
  <pre><code>    "createdCollectionAutomatically" : false,
      "numIndexesBefore" : 1,
      "numIndexesAfter" : 2,
      "ok" : 1
  }
  </code></pre>
  
  <p>To be honest I am new to mongodb so this feedback doesn't mean much to me.  I am not new to GIS and mapping but I can't figure this out.  Please help.  To summarize, I'm trying to narrow my query results by using mongo's geo filtering functions.</p>