按键NOT值的MongoDb MapReduce组

时间:2012-02-01 15:53:56

标签: mongodb mapreduce

我正在尝试编写mapreduce函数来累积来自mongodb的统计信息。但是..我创建数据结构的队友保存了以下数据:

"statistics": {
    "20111206": {
      "CN": {
        "Beijing": {
          "cart": 1,
          "cart_users": [
            { "$oid" : "4EDD73938EAD0E5420000000" }
          ],
          "downloads": {
            "wmv": {
              "mid": 1
            }
          },
          "orders": {
            "wmv": {
              "mid": 1
            }
          }
        }
      }
    }
}

问题是我需要分组的很多值只是存储在键中(例如CN或BEJING)。这些可以是国家代码,视频格式等...所以我不想在mapreduce函数中对这些中的任何一个进行编码。

我用于reduce部分的forEach函数只传递值作为参数..

所以问题是:有没有办法对此进行mapReduce和按键分组,或者我必须先将数据转换为一个看起来或多或少的新结构:< / p>

{
  "movie_id": "4edcd4f29a4e61c00c000059",
  "country": "CN",
  "city": "Beijing",
  "list": [
    {
      "user_id": { "$oid" : "4EDD75388EAD0E5720010000" },
      "downloads": {
        "cnt": 1,
        "list": [
          {
            "format": "wmv",
            "quality": "high"
          }
        ]
      },
      "orders": {
        "cnt": 1,
        "list": [
          {
            "format": "wmv",
            "quality": "high"
          }
        ]
      }
    }
  ]
}

1 个答案:

答案 0 :(得分:0)

假设您的收藏集设置了如下记录:

> db.test_col.findOne()
{
    "_id" : ObjectId("4f90ed994d2246dd7996e042"),
    "statistics" : {
        "20111206" : {
            "CN" : {
                "Beijing" : {
                    "cart" : 1,
                    "cart_users" : [
                        {
                            "oid" : "4EDD73938EAD0E5420000000"
                        }
                    ],
                    "downloads" : {
                        "wmv" : {
                            "mid" : 1
                        }
                    },
                    "orders" : {
                        "wmv" : {
                            "mid" : 1
                        }
                    }
                }
            }
        }
    }
}

这是一个按国家/地区分组的命令,提供城市列表以及该国家/地区的总计数。它应该让你更接近 试图做的事情:

db.runCommand({ mapreduce: "test_col",
                map: function () {
                    var l0      = this.statistics,
                        date    = Object.keySet(l0)[0],
                        l1      = l0[date],
                        country = Object.keySet(l1)[0],
                        l2      = l1[country],
                        city    = Object.keySet(l2)[0],
                        data      = l2[city];
                    emit(country, { date: date, city: city, data: data });
                },
                reduce: function (country, values) {
                    var r = { cities: [], count: 0 };
                    values.forEach(function (v) {
                        if (r.cities.indexOf(v.city) == -1)         r.cities.push(v.city);
                        r.count++;
                    });
                    return r;
                },
                out: { reduce: "test_col_reduce" }
});

我的测试数据的输出如下所示:

> db.test_col_reduce.find()
{ "_id" : "AR", "value" : { "cities" : [ "San Juan", "Buenos Aires", "Cordoba", "Rosario" ], "count" : 18 } }
{ "_id" : "BZ", "value" : { "cities" : [ "Morico", "San Ignacio", "Corozal" ], "count" : 15 } }
{ "_id" : "CN", "value" : { "cities" : [ "Beijing", "Shanghai", "HongKong" ], "count" : 26 } }
{ "_id" : "US", "value" : { "cities" : [ "San Diego", "Los Angeles", "San Francisco", "New York" ], "count" : 27 } }