Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use Map/Reduce in MongoDB?

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.

like image 395
Adam Avatar asked Jun 13 '11 16:06

Adam


People also ask

How does MapReduce work explain with example?

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.

What is the difference between MapReduce function and aggregate function?

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.

What is reduce in MongoDB?

$reduce. Applies an expression to each element in an array and combines them into a single value.


2 Answers

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

enter image description here

For more info, check out this

like image 146
RameshVel Avatar answered Oct 13 '22 01:10

RameshVel


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.

like image 41
Gates VP Avatar answered Oct 12 '22 23:10

Gates VP