Question

Here is the output from my mongodb shell of a very simple example of a $geoWithin query. As you can see, I have only a single GeoJson Polygon in my collection, and each of its coordinates lies within the described $box. Furthermore, the GeoJson seems valid, as the 2dsphere index was created without error.

> db.Townships.find()
{ "_id" : ObjectId("5310f13c9f3a313af872530c"), "geometry" : { "type" : "Polygon",     "coordinates" : [ [ [ -96.74084500000001, 36.99911500000002 ], [ -96.74975600000002,  36.99916100000001 ], [ -96.74953099999998,     36.99916000000002 ], [ -96.74084500000001, 36.99911500000002 ] ] ] }, "type" : "Feature" }
> db.Townships.ensureIndex( { "geometry" : "2dsphere"})
> db.Townships.find( { "geometry" : { $geoWithin : { "$box" : [[-97, 36], [-96, 37]] } } } ).count()
0

Thanks for any advice.

Was it helpful?

Solution

From documentation:

The $box operator specifies a rectangle for a geospatial $geoWithin query. The query returns documents that are within the bounds of the rectangle, according to their point-based location data. The $box operator returns documents based on grid coordinates and does not query for GeoJSON shapes.

If you insert this document...

db.Townships.insert(
{ "geometry" : [ -96.74084500000001, 36.99911500000002 ],
  "type" : "Feature" 
})

...your query will found it (but without index support).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top