0

I'm new to mongodb and amazed by the fact that it handles geospatial coordinates and indexing/searching beautifully. However I have a scenario where any help would be appreciated.

I am developing an application that let's user find buses originating around a place and going to/around other place. I store the coordinates of every bus ride's starting point and ending point.

What I need is to find out is: any buses originating, say within 15 km radius and going to or around another place.

How do I do the query in mongodb/java?

My data is something like this (stores earth location coordinates): collection: travels

entry 1: { origin: 'x1', destination: 'y6', originCoords: [20,10], destCoords: [40,70] } 
entry 2: { origin: 'x2', destination: 'y5', originCoords: [21,11], destCoords: [41,71] } 
entry 3: { origin: 'x3', destination: 'y3', originCoords: [22,12], destCoords: [42,72] } 
entry 4: { origin: 'x4', destination: 'y4', originCoords: [23,13], destCoords: [43,73] } 
entry 5: { origin: 'x5', destination: 'y5', originCoords: [24,14], destCoords: [44,74] } 
entry 6: { origin: 'x6', destination: 'y6', originCoords: [25,15], destCoords: [45,75] } 

Now let's assume that

  1. x1 and x2 cities are located within 5 KM radius of each other.
  2. y5 and y6 cities are also located within 5 km radius of each other.

Now, if I want to search any buses originating around (5 km radius of) x1 and going to around (5 km radius of) y6 then how should I do that (assuming mongodb and java)?

Result must return entry1 and entry2.

Any suggestions or help would be much appreciated.

  • This is two queries whatever way you shake it. What you are trying to do is find the locations (noting two) that are "nearest" to two other locations. Noting that geospatial queries are a kind of "fuzzy match" by nature, they are intended (as in "near" queries) to find more than one match for a given point or polygon or whatever. But you can't get 2 points in one query. Not in the way you are asking anyway. Use two queries and return the top result from each in your return data. – Neil Lunn Apr 18 '14 at 11:19
  • Thanks for the inputs. Got it working by using $and and $geoWithin – user2312798 Apr 22 '14 at 12:03

0 Answers0