Search code examples
mongodbgeolocationmongoidnosql

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

Solution

  • 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.