简体   繁体   English

如何在 MongoDB 中使用 Map/Reduce?

[英]How do I use Map/Reduce in MongoDB?

I'm having trouble wrapping my head around how map/reduce works in MongoDB.我很难理解 map/reduce 在 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 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.我希望能够查询给定 state 中的所有县或市。 So basically some sort of a query that looks for all records where "State=MI".所以基本上某种查询会查找“State=MI”的所有记录。 In this case, there are about 900 records returned.在这种情况下,大约有 900 条记录被返回。 How do I group them by county so that I just get the 83 counties for that state?如何按县对它们进行分组,以便我只获得 state 的 83 个县? 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.我不想使用 distinct,因为我希望能够按字母顺序对它们进行排序,并可能拉出纬度/经度。

Any advice on how to use map/reduce to accomplish this?关于如何使用 map/reduce 来完成此任务的任何建议? I feel like it's rather basic, I just can't figure it out.我觉得它很基本,我只是想不通。

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..我在 PHP 方面没有任何经验,但如果你想了解 MongoDB Map-Reduce 的工作原理,那么你可以看看这个。

在此处输入图像描述

For more info, check out this有关更多信息,请查看

How do I group them by county so that I just get the 83 counties for that state?如何按县对它们进行分组,以便我只获得 state 的 83 个县?

As you describe it, this would require a distinct command .正如您所描述的,这将需要一个独特的 command

I don't want to use distinct as I would like to be able to order them in alphabetical order我不想使用 distinct 因为我希望能够按字母顺序排列它们

The distinct command will return an array of results. distinct命令将返回一个结果数组。 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?关于如何使用 map/reduce 来完成此任务的任何建议?

At this point, you have to use distinct or a similar Map/Reduce operation.此时,您必须使用distinct或类似的 Map/Reduce 操作。

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.如果您可以提供一些示例输入和预期输出,那么我也许可以提供更具体的答案。

I am beginner with MongoDB MapReduce programming, but will attempt to see if it helps, define Map Reduce Function,我是 MongoDB MapReduce 编程的初学者,但会尝试看看它是否有帮助,定义 Map 减少 Z86408593C234AF725FDD1ZB80,

Your Map function would look like:您的 Map function 看起来像:

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

Your Reduce function would look like:您的 Reduce function 看起来像:

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.并将您的查询条件与 Map Reduce 一起传递,上面的伪代码应该为您提供每个县的记录数和县本身的列表。 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.如果你只需要县,你可以简单地用你的减少 function 跳过聚合,在这种情况下你应该得到县列表。

Let me know if there is a better way to achieve.让我知道是否有更好的方法来实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM