最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Mongo聚合:将值划分为组(按分区)

SEO心得admin74浏览0评论
本文介绍了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 和 $ max 获取分区边界和 $ sum 来计算分区元素

$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

发布评论

评论列表(0)

  1. 暂无评论