【问题标题】:Translate aggregation operation in MongoDB to MapReduce将 MongoDB 中的聚合操作转换为 MapReduce
【发布时间】:2020-08-20 02:02:18
【问题描述】:

几天来,我一直在尝试将此查询转换为 MapReduce。具体来说,我需要弄清楚有多少辆不同的汽车行驶了“N”公里。

查询:

db.adsb.group({
    "key": {
        "KM": true
    },
    "initial": {
        "countCar": 0
    },
    "reduce": function(obj, prev) {
        if (obj.Matricula != null) if (obj.Matricula instanceof Array) prev.countCar += obj.Matricula.length;
        else prev.countCar++;
    },
    "cond": {
        "KM": {
            "$gt": 10000,
            "$lt": 45000
        }
    }
});

Mongo 中的每个文档都有这种形式:

{
        "_id" : ObjectId("5a8843e7d79a740f272ccc0a"),
        "KM" : 45782,
        "Matricula" : "3687KTS",
}

我想得到类似的东西:

 /* 0 */
 {
     “KM” : 45000,
     “total” : 634
 }

 /* 1 */
 {
     “KM” : 46000,
     “total” : 784
 }

我的代码在下面,它可以编译但没有给我预期的结果。

特别是,每次我输入“减少”时,似乎都会将所有值重置为 0,这会阻止我累积注册。 我的问题之一是在处理大量信息时,该函数必须迭代几次“reduce”。 我也不知道是否可以这样做,或者我需要在“减少”中返回一个车牌列表及其计数器;然后在 finalize 中全部加起来。

// Map function
var m = function() {
  if (this.KM > 10000 && this.KM < 45000) { // So that i can get KM grouped together by thousands (10000, 20000, 30000...)
    var fl = Math.round(this.KM / 1000) * 1000;
    var car = this.Matricula
    emit (fl, car);
    //print("map KM=" + fl + " Matricula= " + car);
  }
};

// Reduce function
var r = function(key, values) {
    var ya_incluido = false;
    var cars_totales = 0;
    var lista_car = new Array();

    //print( key + " ---- " + values);

    for (var i=0; i < values.length;i++)
    {
            for (var j=0; j < lista_car.length;j++)
            {
                    if(values[i] == lista_car[j]) { //If it is already included, don't aggregate it
                            ya_incluido = true;
                    }

            } if (ya_incluido != true) { //If it is not included, add it to lista_av list.
                    lista_car.push(values[i]);
            } ya_incluido = false;
    }

    cars_totales = lista_av.length; //The number of distinct cars is equal to the lenght of the list we created
    return cars_totales;

};


// Finalize function
var f = function(key,value) {
  // Sum up the results?

}


db.runCommand( {
                 mapReduce: "dealer",
                 map: m,
                 reduce: r,
                 finalize: f,
                 out: {replace : "result"}
               } );

我在这里找到了答案和一个非常好的解释: https://stackoverflow.com/a/27532153/13474284

【问题讨论】:

    标签: javascript mongodb list mapreduce


    【解决方案1】:

    我在这里找到了答案和一个非常好的解释:https://stackoverflow.com/a/27532153/13474284

    我找不到在“减少”中返回来自“地图”的相同内容的方法。而且由于运行了多次,所以只得到了最后一次迭代的结果。它在链接中出现的方式,问题解决了没有任何困难。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-23
      • 1970-01-01
      • 2022-01-22
      • 2019-03-15
      • 1970-01-01
      • 2021-11-25
      • 2020-06-05
      • 2015-10-09
      相关资源
      最近更新 更多