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

编程入门 行业动态 更新时间:2024-10-13 20:14:24
本文介绍了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

更多推荐

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

本文发布于:2023-11-30 16:56:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650780.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分区   划分为   Mongo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!