MongoDB MapReduce为每个文档生成不同的结果

时间:2013-05-24 12:33:47

标签: mongodb mapreduce

这是this question的后续行动,我试图用聚合框架解决这个问题。不幸的是,我必须等待才能将这个特定的mongodb安装更新到包含聚合框架的版本,因此必须使用MapReduce进行这种相当简单的透视操作。

我输入的数据格式如下,每日转储次数为多次:

"_id" : "daily_dump_2013-05-23",
    "authors_who_sold_books" : [
        {
            "id" : "Charles Dickens",
            "original_stock" : 253,
            "customers" : [
                {
                   "time_bought" : 1368627290,
                   "customer_id" : 9715923
                }
            ]
        },
        {
            "id" : "JRR Tolkien",
            "original_stock" : 24,
            "customers" : [
                {
                    "date_bought" : 1368540890,
                    "customer_id" : 9872345
                },
                {
                    "date_bought" : 1368537290,
                    "customer_id" : 9163893
                }
            ]
        }
    ]
}

我是按照以下格式输出的,它在所有每日转储中汇总每个(唯一)作者的所有实例:

{
    "_id" : "Charles Dickens",
    "original_stock" : 253,
    "customers" : [
        {
            "date_bought" : 1368627290,
            "customer_id" : 9715923
        },
        {
            "date_bought" : 1368622358,
            "customer_id" : 9876234
        },
        etc...
    ]
}

我写过这个地图功能......

function map() {
  for (var i in this.authors_who_sold_books)
  {
    author = this.authors_who_sold_books[i];
    emit(author.id, {customers: author.customers, original_stock: author.original_stock, num_sold: 1});
  }
}

...这个减少功能。

function reduce(key, values) {
  sum = 0
  for (i in values)
  {
    sum += values[i].customers.length
  }
  return {num_sold : sum};
}

然而,这给了我以下输出:

{
  "_id" : "Charles Dickens",
  "value" : {
    "customers" : [
      {
        "date_bought" : 1368627290,
        "customer_id" : 9715923
      },
      {
        "date_bought" : 1368622358,
        "customer_id" : 9876234
      },
    ],
    "original_stock" : 253,
    "num_sold" : 1
  }
}
{ "_id" : "JRR Tolkien", "value" : { "num_sold" : 3 } }
{
  "_id" : "JK Rowling",
  "value" : {
    "customers" : [
      {
        "date_bought" : 1368627290,
        "customer_id" : 9715923
      },
      {
        "date_bought" : 1368622358,
        "customer_id" : 9876234
      },
    ],
    "original_stock" : 183,
    "num_sold" : 1
  }
}
{ "_id" : "John Grisham", "value" : { "num_sold" : 2 } }

偶数索引文档列出了customers和original_stock,但num_sold的总和不正确。 奇数索引文档仅列出num_sold,但它是正确的数字。

有人可以告诉我这是什么我错过了吗?

1 个答案:

答案 0 :(得分:1)

你的问题是由于reduce函数输出的格式应该与map函数的格式相同(参见requirements for the reduce function的解释)。

您需要将代码更改为以下内容以解决问题:

function map() {
  for (var i in this.authors_who_sold_books)
  {
    author = this.authors_who_sold_books[i];
    emit(author.id, {customers: author.customers, original_stock: author.original_stock, num_sold: author.customers.length});
  }
}

function reduce(key, values) {
  var result = {customers:[] , num_sold:0, original_stock: (values.length ? values[0].original_stock : 0)};
  for (i in values)
  {
    result.num_sold += values[i].num_sold;
    result.customers = result.customers.concat(values[i].customers);
  }
  return result;
}

我希望有所帮助。

注意:地图功能中的更改num_sold: author.customers.length。我认为这就是你想要的