I'm having trouble wrapping my head around how map/reduce works in MongoDB. I have a collection with the fields: areacode, state, county, zip, city, lat, lon
that lists every zip code in the US along with the corresponding county, state, etc.
I'd like to be able to query say all the counties or cities in a given state. So basically some sort of a query that looks for all records where "State=MI". In this case, there are about 900 records returned. How do I group them by county so that I just get the 83 counties for that state? I don't want to use distinct as I would like to be able to order them in alphabetical order and possibly pull out the lat/long as well.
Any advice on how to use map/reduce to accomplish this? I feel like it's rather basic, I just can't figure it out.
MapReduce is a programming framework that allows us to perform distributed and parallel processing on large data sets in a distributed environment. MapReduce consists of two distinct tasks – Map and Reduce. As the name MapReduce suggests, the reducer phase takes place after the mapper phase has been completed.
Map-reduce is a common pattern when working with Big Data – it's a way to extract info from a huge dataset. But now, starting with version 2.2, MongoDB includes a new feature called Aggregation framework. Functionality-wise, Aggregation is equivalent to map-reduce but, on paper, it promises to be much faster.
$reduce. Applies an expression to each element in an array and combines them into a single value.
I don't have any experience in PHP, but if you wanted to learn how MongoDB Map-Reduce works, then you can check this out..
For more info, check out this
How do I group them by county so that I just get the 83 counties for that state?
As you describe it, this would require a distinct command.
I don't want to use distinct as I would like to be able to order them in alphabetical order
The distinct
command will return an array of results. Sorting these should be trivial, if they're not already sorted.
... and possibly pull out the lat/long as well.
What lat/long do you want to pull out? Based on your schema, you have lat/long for cities, but I don't see the lat/long for a county.
Any advice on how to use map/reduce to accomplish this?
At this point, you have to use distinct
or a similar Map/Reduce operation.
However, I'm not clear that this will provide exactly what you're looking for as I'm not clear on how you intend to extract lat/long data for a county when all you have is city data.
If you can provide some sample inputs and expected outputs, then I may be able to provide a more specific answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With