mongodb map-reduce输出加倍

时间:2011-12-28 13:17:50

标签: mongodb mapreduce

我的源数据库有像..

这样的文档
{date:14, month:1, year:2011, name:"abc", item:"A"}
{date:14, month:1, year:2011, name:"abc", item:"B"}
{date:14, month:1, year:2011, name:"def", item:"A"}
{date:14, month:1, year:2011, name:"def", item:"B"}
{date:15, month:1, year:2011, name:"abc", item:"A"}
{date:16, month:1, year:2011, name:"abc", item:"A"}
{date:15, month:1, year:2011, name:"def", item:"A"}
{date:16, month:1, year:2011, name:"def", item:"A"}

我的地图缩小就像......

var m = function(){    
            emit({name:this.name, date:this.date, month:this.month, year:this.year},        {count:1})
        }
var r = function(key, values) {
        var total_count = 0;
        values.forEach(function(doc) {
            total_count += doc.count;
            });
    return {count:total_count};
};

var res = db.source.mapReduce(m, r, { out : { "merge" : "bb_import_trend" } } );

所以“myoutput”系列会有这样的计数......

{ "_id" : { date : 14,
    month : 1,
        year : 2011,
        name : "abc"
      },
  "_value" : { "count" : 2 }
},
{ "_id" : { date : 14,
    month : 1,
        year : 2011,
        name : "def"
      },
  "_value" : { "count" : 2 }
},
{ "_id" : { date : 15,
    month : 1,
        year : 2011,
        name : "abc"
      },
  "_value" : { "count" : 1 }
},
{ "_id" : { date : 15,
    month : 1,
        year : 2011,
        name : "def"
      },
  "_value" : { "count" : 1 }
},
{ "_id" : { date : 16,
    month : 1,
        year : 2011,
        name : "abc"
      },
  "_value" : { "count" : 1 }
},
{ "_id" : { date : 16,
    month : 1,
        year : 2011,
        name : "def"
      },
  "_value" : { "count" : 1 }
}

总共6个文件

所以当另外三个日期17的文件像...

{date:17, month:1, year:2011, name:"abc", item:"A"}
{date:17, month:1, year:2011, name:"abc", item:"B"}
{date:17, month:1, year:2011, name:"def", item:"A"}

已被添加到我的源集合中...并且我正在运行map-reduce它应该是jus添加两个文件,如...

前6加

...
{ "_id" : { date : 17,
    month : 1,
        year : 2011,
        name : "abc"
      },
  "_value" : { "count" : 2 }
},
{ "_id" : { date : 17,
    month : 1,
        year : 2011,
        name : "def"
      },
  "_value" : { "count" : 1 }
}

但不是这样,它复制了之前的所有6个文档,并添加了另外2个新文档..在我的输出集合中总共有14个文档(尽管我在输出中使用了合并)。

注意> :当我使用具有相同源集合的另一个数据库并执行相同的过程时,它给出了我想要的输出(只有8个集合)。但我的gui使用的数据库仍然是旧的。[/ p>

0 个答案:

没有答案