johnrees
johnrees

Reputation: 327

MongoDB geoNear multiple coordinates

I want to order my results based on their proximity to MULTIPLE points in a 2D space.

I assume this would be done by querying against the first point and then re-querying/checking those results against the second point?

Maybe the code below explains what I am trying to achieve a bit better?

db.runCommand({
  geoNear:"places",
  near:[ [52.5243, 13.4063], [48.1448, 11.5580] ]
})

Solution: Incase anyone is interested, this is how I achieved this (thanks to the answer below)

a = Trip.geo_near([52.5243, 13.4063], :max_distance => 40, :unit => :mi).uniq
b = Trip.geo_near([48.1448, 11.5580], :max_distance => 40, :unit => :mi).uniq
@results = a & b

Upvotes: 2

Views: 2482

Answers (2)

Dylan Bathurst
Dylan Bathurst

Reputation: 109

MongoDB has a whole section in their documentation on Geospacial indexing. http://www.mongodb.org/display/DOCS/Geospatial+Indexing

I think what you're looking for is a bounding box query. This is directly from their code examples.

box = [[40.73083, -73.99756], [40.741404,  -73.988135]]
db.places.find({"loc" : {"$within" : {"$box" : box}}})

Upvotes: 4

A. Jesse Jiryu Davis
A. Jesse Jiryu Davis

Reputation: 24027

What do you intend the query above to return? Places that are near one OR the other location? In that case, you should run two queries, then union the results in your application code.

Upvotes: 2

Related Questions