Mongo中的地图聚合结果

时间:2016-08-08 13:34:32

标签: mongodb mongoose aggregation-framework

以下是数据集:

interface{}

我必须按{ "_id" : "1", "key" : "111", "payload" : 100, "type" : "foo", "createdAt" : ISODate("2016-07-08T11:59:18.000Z") } { "_id" : "2", "key" : "111", "payload" : 100, "type" : "bar", "createdAt" : ISODate("2016-07-09T11:59:19.000Z") } { "_id" : "3", "key" : "222", "payload" : 100, "type" : "foo", "createdAt" : ISODate("2016-07-10T11:59:20.000Z") } { "_id" : "4", "key" : "222", "payload" : 100, "type" : "foo", "createdAt" : ISODate("2016-07-11T11:59:21.000Z") } { "_id" : "5", "key" : "222", "payload" : 100, "type" : "bar", "createdAt" : ISODate("2016-07-12T11:59:22.000Z") } 分组:

key

产生下一组:

db.items.aggregate([{$group: {_id: {key: '$key'}}}])

之后,我必须为每个组记录检索{ "_id" : { "key" : "111" } } { "_id" : { "key" : "222" } } foo的最新值。 我的问题是最佳方式是什么?我可以在javascript中迭代这些项目,并为每个组结果执行额外的往返数据包。但我不确定它是否具有时间效率。

1 个答案:

答案 0 :(得分:2)

我不确定最佳方式,但最简单的方法是扩展聚合管道,如

db.items.aggregate([
  {
    $group: 
      {
        _id: { key: "$key", type: "$type" }, 
        last: { $max: "$createdAt" } 
      }
  }, 
  { 
    $group: 
    { 
      _id: { key: "$_id.key" }, 
      mostRecent: { $push: { type: "$_id.type", createdAt: "$last" } } 
    } 
  } 
]);

对于您的文档集合,将导致

{ "_id" : { "key" : "222" }, "mostRecent" : [ { "type" : "bar", "createdAt" : ISODate("2016-07-12T11:59:22Z") }, { "type" : "foo", "createdAt" : ISODate("2016-07-11T11:59:21Z") } ] }
{ "_id" : { "key" : "111" }, "mostRecent" : [ { "type" : "bar", "createdAt" : ISODate("2016-07-09T11:59:19Z") }, { "type" : "foo", "createdAt" : ISODate("2016-07-08T11:59:18Z") } ] }
相关问题