Adam
Adam

Reputation: 1477

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.

Upvotes: 10

Views: 9590

Answers (3)

RameshVel
RameshVel

Reputation: 65887

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

Upvotes: 44

mailboat
mailboat

Reputation: 1077

I am beginner with MongoDB MapReduce programming, but will attempt to see if it helps, define Map Reduce Function,

Your Map function would look like:

function() { 
   emit( this.county, {count: 1} ); 
}

Your Reduce function would look like:

function(key, values) { 
    var result = {count: 0};
    //Skip aggregation step if not needed 
    values.forEach(function(value) {     
       result.count += value.count;
    }); 
    return result;  
}

And pass your query condition along with Map Reduce, the above pseudo code should give you # of records for each county & list of county itself. if you need just the counties, you can simply skip aggregation with your reduce function, in which you case you should get list of counties.

Let me know if there is a better way to achieve.

Upvotes: 0

Gates VP
Gates VP

Reputation: 45307

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.

Upvotes: 3

Related Questions