在汇总框架中提供完整时间戳时,如何按日期汇总?

编程入门 行业动态 更新时间:2024-10-09 07:27:34
本文介绍了在汇总框架中提供完整时间戳时,如何按日期汇总?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个错误集合,因此每个错误都包含一个date字段.如何仅按DAY(即排除一天中的时间)汇总/计数/分组错误?我猜应该应用一些精巧的投影.

I have a collection of errors, so that every error carries a date field. How can I aggregate/count/group the errors by DAY only (i.e. exclude the time of the day)? I guess, some smart projection should be applied.

推荐答案

您可以通过使用以下聚合运算符来做到这一点:

You can do this by using the following aggregation operators:

  • $ group
  • $ year
  • $ month
  • $ dayOfMonth
  • $group
  • $year
  • $month
  • $dayOfMonth

这会为您提供每个日期的错误计数:

This gives you the error count for each date:

db.errors.aggregate( { $group : { _id: { year : { $year : "$date" }, month : { $month : "$date" }, day : { $dayOfMonth : "$date" }, }, count: { $sum: 1 } }} );

此示例假定错误文档中的日期字段为date,类型为 BSON日期. 文档明确建议不要在MongoDB中使用Timestamp类型,但强烈建议不要使用此类型:

This example assumes that the date field in your error documents is date and of type BSON Date. There is also a Timestamp type in MongoDB, but use of this type is explicitely discouraged by the documentation:

注意:BSON时间戳类型供内部MongoDB使用.对于大多数 情况下,在应用程序开发中,您将需要使用BSON日期 类型.有关更多信息,请参见日期.

Note: The BSON Timestamp type is for internal MongoDB use. For most cases, in application development, you will want to use the BSON date type. See Date for more information.

更多推荐

在汇总框架中提供完整时间戳时,如何按日期汇总?

本文发布于:2023-11-22 00:32:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615284.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:框架   完整   按日   时间

发布评论

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

>www.elefans.com

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