帮助 MongoDB 中的地图缩减

发布于 2024-11-24 16:32:07 字数 634 浏览 7 评论 0原文

我正在努力牢牢掌握 MapReduce 的工作原理以及何时使用它。我得到了一些没有意义的随机结果,但也许我对 MapReduce 的理解是错误的?

这是我正在做的一个例子。

我收集了超过 15000 个英国城镇,其结构如下:

{
  "_id" : ObjectId("4e234105e138231a7f000004"),
  "county" : "Powys",
  "name" : "Abbey-Cwmhir",
  "location" : {
    "latitude" : 52.3298355191946,
    "longitude" : -3.39230306446552
  }
}

每个县都有许多城镇,我希望为每个县获得一个具有以下结构的新集合;

{
  "_id" : "Powys",
  "towns" : [
    {
      "name" : "Abbey-Cwmhir",
      "loc" : [52.3298355191946, -3.39230306446552]
    },
    //.. etc.
  ]
}

所以,我想 MapReduce 是一个理想的选择,对吧?如果是的话,正确的map和reduce函数是什么?

I'm struggling to get a firm grasp of how map reduce works and when to use it. I'm getting some random results that just isn't making sense, but maybe my understanding of mapreduce in wrong?

Here's an example of what I am doing.

I have a collection of over 15000 uk towns with the following structure;

{
  "_id" : ObjectId("4e234105e138231a7f000004"),
  "county" : "Powys",
  "name" : "Abbey-Cwmhir",
  "location" : {
    "latitude" : 52.3298355191946,
    "longitude" : -3.39230306446552
  }
}

Each county has many towns, and I would like to get a new collection with the following structure for each county;

{
  "_id" : "Powys",
  "towns" : [
    {
      "name" : "Abbey-Cwmhir",
      "loc" : [52.3298355191946, -3.39230306446552]
    },
    //.. etc.
  ]
}

So, I guess map reduce is an ideal candidate for this right? If it is, how what would be the correct map and reduce functions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-12-01 16:32:07

作为起点,您可以使用以下内容:

Map 函数

function() {
  emit( this.county,{ 
          towns: [ 
            { 
              name: this.name, 
              loc: this.location 
            }
          ]
        } ); 
}

Reduce 函数

function(key, values) {
  result = { towns: [] }; 
  values.forEach( 
    function( townsgroup ) {
      townsgroup.towns.forEach( 
        function( town ) {
          result.towns.push( town );
        });
    });
  return result;
}

感谢dcrosta 的更正。

As a starting point you could use something like this:

Map function

function() {
  emit( this.county,{ 
          towns: [ 
            { 
              name: this.name, 
              loc: this.location 
            }
          ]
        } ); 
}

Reduce function

function(key, values) {
  result = { towns: [] }; 
  values.forEach( 
    function( townsgroup ) {
      townsgroup.towns.forEach( 
        function( town ) {
          result.towns.push( town );
        });
    });
  return result;
}

Thank you dcrosta for the correction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文