Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb Mongo嵌套$group计算年份和月份_Mongodb_Mongodb Query_Aggregation Framework - Fatal编程技术网

Mongodb Mongo嵌套$group计算年份和月份

Mongodb Mongo嵌套$group计算年份和月份,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,这是我的一个示例订单文档 {startDate: “2010:10:10”, numberOfHours: 10}, {startDate: “2010:11:10”, numberOfHours: 5}, {startDate: “2011:12:10”, numberOfHours: 1}, {startDate: “2012:10:10”, numberOfHours: 10} 首先,我想计算订单数量(totalOrders)和开始日期每年的numberOfHours之和(totalHo

这是我的一个示例订单文档

{startDate: “2010:10:10”, numberOfHours: 10},
{startDate: “2010:11:10”, numberOfHours: 5},
{startDate: “2011:12:10”, numberOfHours: 1},
{startDate: “2012:10:10”, numberOfHours: 10}
首先,我想计算订单数量(totalOrders)和开始日期每年的numberOfHours之和(totalHours)。然后,对于一年中的每个月,我需要计算订单数量(订单)和numberOfHours(小时)的总和。输出应该如下所示

    [
    // Current year first
   {
     year: 2019,
     totalOrders: 120,
     totalHours: 1234,
     months: [
       { 
          month: 0, // 0 based month index so jan = 0
          orders: 12, 
          hours: 120 
      },
{ 
          month: 1, 
          orders: 5, 
          hours: 100 
      }
      ////////////
      ]
    },
// 2018 etc
]
我查看了嵌套的@group示例,但找不到匹配项。我知道如何将年和月分组如下

const result = await this._collection.aggregate([
            {
                $project: {
                    startYear: { $substr: ["$startDate", 0, 4] },
                    startMonth: { $substr: ["$startDate", 5, 2] }
                }
            },
            {
                $group: {
                    _id: { year: "$startYear", month: "$startMonth" },
                    orders: { $sum: 1 },
                    hours: { $sum: "$numberOfHours" }
                },
            },
        ]).toArray();
你知道如何进行我提到的输出吗?
我们将非常感谢您的帮助

基本上,您可以添加另一个
$group
,只需
$push
每年将月份放入一个数组中:

const result = await this._collection.aggregate([
    {
        $project: {
            startYear: { $substr: ["$startDate", 0, 4] },
            startMonth: { $substr: ["$startDate", 5, 2] }
        }
    },
    {
        $group: {
            _id: { year: "$startYear", month: "$startMonth" },
            orders: { $sum: 1 },
            hours: { $sum: "$numberOfHours" }
        },
     },
     {
         $group: {
             _id: { year: "$_id.year" },
             totalOrders: { $sum: "$orders" },
             totalHours: { $sum: "$hours" },
             months: {
               $push: {
                 month: "$_id.month",
                 orders: "$orders",
                 hours: "$hours"
               }
             }
         }
     }
]).toArray();
您可以更好地使用运算符,并可以使用任何您想要的格式

db.collection.aggregate([
  { "$group": {
    "_id": {
      "month": { "$month": { "$dateFromString": { "dateString": "$startDate", "format": "%Y:%m:%d" }}},
      "year": { "$year": { "$dateFromString": { "dateString": "$startDate", "format": "%Y:%m:%d" }}}
    },
    "hours": { "$sum": "$numberOfHours" },
    "count": { "$sum": 1 }
  }},
  { "$group": {
    "_id": "$_id.year",
    "totalHours": { "$sum": "$hours" },
    "totalOrders": { "$sum": "$count" },
    "months": {
      "$push": {
        "month": "$_id.month",
        "order": "$count",
        "hours": "$hours"
      }
    }
  }}
])