如何在MongoDB中聚合相似的对象?

时间:2014-01-02 16:16:53

标签: mongodb

给出以下样本数据:

{ "_id" : ObjectId("528f6325bc0cb945ffbfd7d2"), "Value1" : 297138463, "hostname" : "server1", "time" : 1385128741.221214 }
{ "_id" : ObjectId("528f6325bc0cb945ffbfd7d3"), "Value1" : 312283635, "hostname" : "server2", "time" : 1385128741.24351 }
{ "_id" : ObjectId("528f6325bc0cb945ffbfd7d4"), "Value1" : 277946406, "hostname" : "server3", "time" : 1385128741.249723 }
{ "_id" : ObjectId("528f6325bc0cb945ffbfd7d5"), "Value1" : 300892042, "hostname" : "server4", "time" : 1385128741.255984 }
{ "_id" : ObjectId("528f6325bc0cb945ffbfd7d6"), "Value1" : 294501471, "hostname" : "server5", "time" : 1385128741.263583 }
{ "_id" : ObjectId("528f6361bc0cb947df5ff960"), "Value1" : 297187182, "hostname" : "server1", "time" : 1385128801.826883 }
{ "_id" : ObjectId("528f6361bc0cb947df5ff961"), "Value1" : 312331528, "hostname" : "server2", "time" : 1385128801.848045 }
{ "_id" : ObjectId("528f6361bc0cb947df5ff962"), "Value1" : 277987001, "hostname" : "server3", "time" : 1385128801.857552 }
{ "_id" : ObjectId("528f6361bc0cb947df5ff963"), "Value1" : 300924710, "hostname" : "server4", "time" : 1385128801.864611 }
{ "_id" : ObjectId("528f6361bc0cb947df5ff964"), "Value1" : 294528575, "hostname" : "server5", "time" : 1385128801.8726 }
{ "_id" : ObjectId("528f639ebc0cb949ef5db1d4"), "Value1" : 297212963, "hostname" : "server1", "time" : 1385128862.060666 }
{ "_id" : ObjectId("528f639ebc0cb949ef5db1d5"), "Value1" : 312371013, "hostname" : "server2", "time" : 1385128862.083595 }
{ "_id" : ObjectId("528f639ebc0cb949ef5db1d6"), "Value1" : 278014257, "hostname" : "server3", "time" : 1385128862.089885 }
{ "_id" : ObjectId("528f639ebc0cb949ef5db1d7"), "Value1" : 300952539, "hostname" : "server4", "time" : 1385128862.095612 }
{ "_id" : ObjectId("528f639ebc0cb949ef5db1d8"), "Value1" : 294572376, "hostname" : "server5", "time" : 1385128862.103005 }
{ "_id" : ObjectId("528f63d9bc0cb94bc1e8eedd"), "Value1" : 297243502, "hostname" : "server1", "time" : 1385128921.73372 }
{ "_id" : ObjectId("528f63d9bc0cb94bc1e8eede"), "Value1" : 312406080, "hostname" : "server2", "time" : 1385128921.755981 }
{ "_id" : ObjectId("528f63d9bc0cb94bc1e8eedf"), "Value1" : 278041965, "hostname" : "server3", "time" : 1385128921.762048 }
{ "_id" : ObjectId("528f63d9bc0cb94bc1e8eee0"), "Value1" : 301007401, "hostname" : "server4", "time" : 1385128921.767804 }
{ "_id" : ObjectId("528f63d9bc0cb94bc1e8eee1"), "Value1" : 294599791, "hostname" : "server5", "time" : 1385128921.775356 }

我想为彼此之间发生的所有对象聚合Value1。这可能吗? time从纪元开始是UTC时间。

1 个答案:

答案 0 :(得分:1)

如果你想在同一秒内找到所有事件,这将有效,假设一个名为events的集合:

db.events.group({
    $keyf: function (event) {
        return {
            seconds: Math.round(event.time)
        };
    },
    initial: {
        events: []
    },
    $reduce: function (event, result) {
        result.events.push(event);
    }
});
  • $ keyf - 用于从函数而不是现有字段创建密钥,使用此方法将时间舍入/截断为秒
  • initial - 为同一第二个
  • 中的每组记录创建初始缩减文档
  • $ reduce将每个事件对象推送到分组结果
  • 有关更多选项,请参阅group文档

如果汇总你的意思是总和,那么就改成这样的东西:

db.events.group({
    $keyf: function (event) {
        return {
            seconds: Math.round(event.time)
        };
    },
    initial: {
        total: 0
    },
    $reduce: function (event, result) {
        result.total += event.Value1;
    }
});