Mongo聚合:将值划分为组(按分区) [英] Mongo aggregation: partitioning values into groups (by partition)

查看:150
本文介绍了Mongo聚合:将值划分为组(按分区)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此方法,我可以按时间对一组事件文档进行分组.此解决方案返回相同的输入文档,但添加了分区号.

Using this approach, I can group a set of event documents by time. This solution returns the same input documents but with a partition number added.

我该如何做同样的事情,而是返回分区?输出文档示例为:

How can I do the same thing but return the partitions instead? An example output document would be:

{
  partition: 0,
  startDate: ISODate("1900-04-12T18:30:00.000Z"),
  endDate: ISODate("2019-04-12T18:30:00.000Z"),
  numEvents: 27
}

推荐答案

假设这是您聚合的当前输出:

Let's say this is the current output of your aggregation:

{
    "_id" : null,
    "datesWithPartitions" : [
        {
            "date" : ISODate("2019-04-12T18:30:00Z"),
            "partition" : 0
        },
        {
            "date" : ISODate("2019-04-12T20:00:00Z"),
            "partition" : 1
        },
        {
            "date" : ISODate("2019-04-12T20:10:00Z"),
            "partition" : 1
        },
        {
            "date" : ISODate("2019-04-12T21:00:00Z"),
            "partition" : 2
        },
        {
            "date" : ISODate("2019-04-12T21:15:00Z"),
            "partition" : 2
        },
        {
            "date" : ISODate("2019-04-12T21:45:00Z"),
            "partition" : 3
        },
        {
            "date" : ISODate("2019-04-12T23:00:00Z"),
            "partition" : 4
        }
    ]
}

要获取所需格式的数据,您需要附加以下汇总步骤:

To get the data in a format you need you need to append following aggregation steps:

db.col.aggregate([
    {
        $unwind: "$datesWithPartitions"
    },
    {
        $group: {
            _id: "$datesWithPartitions.partition",
            numEvents: { $sum: 1 },
            startDate: { $min: "$datesWithPartitions.date" },
            endDate: { $max: "$datesWithPartitions.date" }
        }
    },
    {
        $project: {
            _id: 0,
            partition: "$_id",
            startDate: 1,
            endDate: 1,
            numEvents: 1
        }
    }
])

$ unwind 将返回单个日期文档,然后您可以将 $ group $ min

$unwind will return single date per document and then you can apply $group with $min and $max to get partition boundaries and $sum to count partition elements

这篇关于Mongo聚合:将值划分为组(按分区)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆