With MongoDB is there a way to perform a bounds query (within a box) and order the results from the center point and have them return with the distance calculation..
I realize doing a near with a radius can provide me with a distance ordered set but I want I’m trying to identify if it is possible within a box not a circle.
Unfortunately, this is not possible exactly as you described. As per the “Bounds Queries” section of the “Geospatial Indexing” documentation: “Results [of $within queries] are not sorted by distance”
http://www.mongodb.org/display/DOCS/Geospatial+Indexing#GeospatialIndexing-BoundsQueries
There are a few possible work-arounds.
1) You can perform a $within query and have your application calculate the distance of each point returned from the center of the box.
2) You could first perform a $within query, save the points in an array, and then run a $near query combined with an $in [].
For example,
Imagine a box with boundaries [0,0] and [8,8]:
and some points:
First the $within query is done, and the points that are returned are saved:
Then a $near query is combined with an $in query:
The above solution was taken from a similar question that was asked on Google Groups:
groups.google.com/group/mongodb-user/browse_thread/thread/22d1f0995a628c84/1f2330694a7cf969
Hopefully this will allow you to perform the operation that you need to do.