Question

I have a collection, name Events:

Each document in Events collection has source and destination in lat-long.

I would like to make a query on the Events collection and get only those events that are within some distance from source and within some distance from destination.

I read that MongoDB does not support two geospatial indexes on one collection.

I am confused as in how my data model should look like and how can I make a query to achieve my purpose?

Thanks

Was it helpful?

Solution

You'll have to work with the geo index limitation, so it leaves you with a couple of options. One is to create two collections and run two queries, and resolve the intersection at the application level. This could be expensive depending on what you're doing.

The other scenario is to work with one collection, but change your query to check $geowithin some geometry which represents the intersection area of the areas around your source and destination. Since you're querying for events that are both within some distance of your source and destination, this implies that there is an intersection area. It is up to you to calculate the intersection geometry. If possible, you can precalculate and store these intersections.

OTHER TIPS

You may only have 1 geospatial index per collection, for now. While MongoDB may allow to create multiple indexes, this behavior is unsupported. Because MongoDB can only use one index to support a single query, in most cases, having multiple geo indexes will produce undesirable behavior

There are a few tickets in JIRA for this that you might want to vote on:

https://jira.mongodb.org/browse/SERVER-2331

https://jira.mongodb.org/browse/SERVER-3653

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top