Is it possible to perform a geospatial search in MongoDB in the resulting area from the difference of two circles. Let's say that I have a circle A of radius x and another circle B of radius y where y > x. I need to find all the points that are inside B - A. Is this possible with MongoDB. I know that MongoDB support polygon searches but maybe there is a better way. Keep in mind that I'm using Mongo from a rails application using mongoid.
1 Answers
Yes,it is possible.
use $within
operator.
http://www.mongodb.org/display/DOCS/Geospatial+Indexing/#GeospatialIndexing-Querying
Bounds Queries
$within can be used instead of $near to find items within a shape. Results are not sorted by distance, which may result in faster queries when this sorting is not required. Shapes of type $box (rectangles), $center (circles), and $polygon (concave and convex polygons) are supported. All bounds queries implicitly include the border of the shape as part of the boundary, though due to floating-point inaccuracy this can't strictly be relied upon.
To query for all points within a rectangle, you must specify the lower-left and upper-right corners: A circle is specified by a center point and radius:
center = [50, 50] radius = 10 db.places.find({"loc" : {"$within" : {"$center" : [center, radius]}}})
One approach would be that you can find points within both circles, then do an intersection of the sets. This would work for both cases, if the circles are concentric, or if not concentric but having some overlapping area.

- 42,059
- 16
- 116
- 175
-
That looks like the ONLY approach possible. There's no operator such as $annularRing to be used with $within. – Aafreen Sheikh Aug 02 '12 at 05:35