【问题标题】:Aggregate Hourly Weekly Monthly Yearly data in mongodb在mongodb中汇总每小时每周每月每年数据
【发布时间】:2022-06-27 20:18:08
【问题描述】:

Q1. 我需要按创建日期和 driverId 过滤数据,然后需要按小时、每周、每月和每年汇总总数。我已经检查过其他解决方案,但没有太大帮助。

样本数据:

    [
      {
        id: "1",
        created : "2022-01-04T03:22:18.739Z",
        completed: "2022-01-06T03:53:28.463Z",
        driverId: "B-72653",
        total: 15,
      },
      {
        id: "2",
        created : "2022-01-01T03:22:18.739Z",
        completed: "2022-01-02T03:53:28.463Z",
        driverId: "B-72653",
        total: 33
      },
      {
        id: "3",
        created : "2021-08-26T01:22:18.739Z",
        completed: "2021-08-26T09:53:28.463Z",
        driverId: "B-72653",
        total: 43
      },  
      {
        id: "4",
        created : "2021-03-26T02:22:18.739Z",
        completed: "2021-03-26T07:53:28.463Z",
        driverId: "B-73123",
        total: 35
      },  
    ]

需要回复:

    {
        Hourly:[10,5,5,6,7,8,4,5,6,3,44,2,1,2,3,44,5,6,75,4,3,2,1], // 24 Hours (Each Hour Total)
        Weekly:[10,30,34,45,56,67,78], // 7 days (Each Day Total)
        Monthly:[10,30,34,45,56,67,78,55,44,33,22,12], // 12 Months (Each Month Total)
        Yearly: [10,30] // Year Total (Each Year Total)
    }

Q2.我们如何过滤嵌套数组副产品>品牌ID并通过其ID获得产品价格的总和并按小时,每周,每月,每年过滤?。

【问题讨论】:

  • this 你在找什么?您的预期输出与您的样本数据不匹配,因此无法验证。
  • 你能解释一下你是如何从输入到输出的吗?不是 mongodb,而是如何在字段上执行操作?
  • @ray 谢谢!!这正是我所需要的。是否可以在每个总数中添加月份,例如每周:[{1:10},{2:32}] 等,因为如果我们使用数组索引,那么如果数组中缺少任何月份,它可能会给出错误的月份信息?
  • 问题的更新似乎是一个更适合新问题的板问题。请帮助提出一个新问题,以保持当前问题的重点。另外,1.不要将代码/数据粘贴为图像2.在新问题中添加预期的输出和解释

标签: mongodb mongodb-query aggregation-framework


【解决方案1】:

您可以使用 $group_id$hour / $week / $month / $year 来聚合总和。 $push 将它们放入一个数组中以获得您的预期结果。 使用$facet 对所有 4 个案例重复该过程。

db.collection.aggregate([
  {
    "$facet": {
      "Hourly": [
        {
          $group: {
            _id: {
              $hour: "$created"
            },
            total: {
              $sum: "$total"
            }
          }
        },
        {
          $sort: {
            _id: 1
          }
        },
        {
          $group: {
            _id: null,
            result: {
              $push: {
                hour: "$_id",
                total: "$total"
              }
            }
          }
        }
      ],
      Weekly: [
        {
          $group: {
            _id: {
              "$week": "$created"
            },
            total: {
              $sum: "$total"
            }
          }
        },
        {
          $sort: {
            _id: 1
          }
        },
        {
          $group: {
            _id: null,
            result: {
              $push: {
                week: "$_id",
                total: "$total"
              }
            }
          }
        }
      ],
      Monthly: [
        {
          $group: {
            _id: {
              $month: "$created"
            },
            total: {
              $sum: "$total"
            }
          }
        },
        {
          $sort: {
            _id: 1
          }
        },
        {
          $group: {
            _id: null,
            result: {
              $push: {
                month: "$_id",
                total: "$total"
              }
            }
          }
        }
      ],
      Yearly: [
        {
          $group: {
            _id: {
              $year: "$created"
            },
            total: {
              $sum: "$total"
            }
          }
        },
        {
          $sort: {
            _id: 1
          }
        },
        {
          $group: {
            _id: null,
            result: {
              $push: {
                year: "$_id",
                total: "$total"
              }
            }
          }
        }
      ]
    }
  },
  {
    "$addFields": {
      "Hourly": {
        "$arrayElemAt": [
          "$Hourly",
          0
        ]
      },
      "Weekly": {
        "$arrayElemAt": [
          "$Weekly",
          0
        ]
      },
      "Monthly": {
        "$arrayElemAt": [
          "$Monthly",
          0
        ]
      },
      "Yearly": {
        "$arrayElemAt": [
          "$Yearly",
          0
        ]
      }
    }
  },
  {
    "$addFields": {
      "Hourly": "$Hourly.result",
      "Weekly": "$Weekly.result",
      "Monthly": "$Monthly.result",
      "Yearly": "$Yearly.result"
    }
  }
])

这是Mongo playground 供您参考。

【讨论】:

  • 我们如何过滤嵌套数组副产品 > 品牌 id 并按小时、每周、每月、每年获得产品价格的总和? mongoplayground.net/p/-4Upnm4E8vj
  • @TahaFarooqui 您可以在开头放置一个$match 阶段,例如this
  • 显示总量。我需要总结相同的产品 ID 价格。 prnt.sc/26eg8cy
  • @TahaFarooqui 您的网址无效。请使用 stackoverflow 图片附加选项更新您的问题
  • 你能检查更新的问题吗?
猜你喜欢
  • 2017-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-09
  • 1970-01-01
相关资源
最近更新 更多