I am trying to achieve something that has proven rather hard with MongoDB.
I have 2 collections. One is called “marker”, the other called “routes” and holds routes that users have created between markers. The route collection has two important fields: [start_marker] and [end_marker]. Those fields are the _ids of the markers in the marker collection.
When browsing a map the app makes a geospatial box query to the marker collection to find and display markers on the map.
When a user clicks a marker a bubble shows the marker name.
Below the name there is also a dynamic field that I am having issues with. This field is supposed to show:
- What markers were driven to from the selected marker on the map
- How many times those markers were driven to
- Only the 3 most frequently driven to markers will be shown in the dynamic field
Since my record collection has the [start_marker] and [end_marker] _ids recorded, I query the record collection for the selected marker’s _id in the [start_marker] field.
This yields a few hundred documents where the selected marker is always in the [start_marker] field. Now, I want to sort which [end_markers] occur the most, count how many times they occur, pick the top 3, take their _ids and query the marker collection with them to extract the marker names.
Problem is, count needs me to know which end marker field to count by, which in turn requires me to know how often it occurs. Catch 22.
End bubble result would look like this:
City 1
City 16 – 256 times
City 21 – 118 times
City 4 – 88 times
I am open to changing the database structure since Mongo is schemaless, maybe add a count field somewhere but I am at a complete loss at this moment. This query would occur a few hundred times per second so map reduce might have a negative impact on database performance. Some input would be greatly appreciated!
I believe I found a simple enough answer.