【问题标题】:MongoDB count value of attribute and group by month and yearMongoDB按月和年统计属性和分组的值
【发布时间】:2021-10-01 07:21:14
【问题描述】:

我的 mongodb 中有文档,我尝试计算我拥有的每一个相同的描述并分类到他们创建的月份。

我想返回一个数组,其中包含对象数组,其中包含月份数和描述值的子数组和计数。

我希望能够选择要计算的描述值并选择他将在哪一年向我显示数据 例如这是我的文件:

    [
  {
    "username": "ron",
    "skills": [
      {
        "rank": "high",
        list: [
          {
            "subject": "Football"
          },
          {
            "subject": "Swim"
          }
        ]
      },
      {
        "rank": "low",
        list: [
          {
            "subject": "Baseball"
          },
          
        ]
      }
    ],
    "duration": 0,
    "date": ISODate("2021-07-24T12:05:27.127Z"),
    "createdAt": ISODate("2021-07-24T12:05:49.985Z"),
    "updatedAt": ISODate("2021-07-24T12:05:49.985Z"),
    "__v": 0
  },
  {
    "username": "john",
    "skills": [
      {
        "rank": "low",
        list: [
          {
            "subject": "Football"
          },
          
        ]
      }
    ],
    "duration": 0,
    "date": ISODate("2021-07-25T12:05:53.000Z"),
    "createdAt": ISODate("2021-07-24T12:05:59.249Z"),
    "updatedAt": ISODate("2021-07-24T12:05:59.249Z"),
    "__v": 0
  },
  {
    "username": "david",
    "skills": [
      {
        "rank": "high",
        list: [
          {
            "subject": "Football"
          },
          {
            "subject": "Baseball"
          }
        ]
      },
      {
        "rank": "low",
        list: [
          {
            "subject": "Swim"
          },
          
        ]
      }
    ],
    "duration": 0,
    "date": ISODate("2021-08-26T12:06:13.000Z"),
    "createdAt": ISODate("2021-07-24T12:06:21.328Z"),
    "updatedAt": ISODate("2021-07-24T12:06:21.328Z"),
    "__v": 0
  },
  {
    "username": "david",
    "skills": [
      {
        "request": "high",
        list: [
          {
            "subject": "Swim"
          },
          
        ]
      },
      {
        "request": "low",
        list: [
          {
            "subject": "Football"
          },
          {
            "subject": "Baseball"
          },
          
        ]
      }
    ],
    "duration": 0,
    "date": ISODate("2021-01-21T13:07:50.000Z"),
    "createdAt": ISODate("2021-07-24T12:08:05.552Z"),
    "updatedAt": ISODate("2021-07-24T12:14:51.285Z"),
    "__v": 0
  },
  {
    "username": "david",
    "skills": [
      {
        "rank": "high",
        list: [
          {
            "subject": "Football"
          },
          
        ]
      },
      
    ],
    "duration": 0,
    "date": ISODate("2022-01-21T13:07:50.000Z"),
    "createdAt": ISODate("2022-07-24T12:08:05.552Z"),
    "updatedAt": ISODate("2022-07-24T12:14:51.285Z"),
    "__v": 0
  }
]

如果我只想匹配等于“足球”或“棒球”的描述,我希望得到的结果:

 {
    [ {"month_number":7,"result":[{"description":'Football',"count":2},{"description":"Baseball","count":1}]},
          {"month_number":8,"result":[{"description":'Football',"count":1}]}]
}

我是 mongodb 的新手......到目前为止,我已经能够计算每个值的数量并只显示我想要的值,但我不知道如何根据我的年份将其分类为几个月选择。

我试过了:

db.exercises.aggregate([{$match:{ $and:[{description:{$in: ["Baseball","Football"]}}]}} ,{$group:{_id:"$description",count:{$sum:1}}}])

还有这个

db.exercises.aggregate(
  [
    {
      $project:
        {
          _id: 0,
          year: { $year: "$date" },
          month: { $month: "$date" },
          description:"$description"
        }
    },
    {
        $match:{$and:[{description:{$in: ["Baseball","Football","Swim"]},year:2021}]}
     },
     {$group:{_id:"$description" , count:{$sum:1}}}
     
  ]
)

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    您需要再添加 1 个分组,即按月。

    工作playground

    db.collection.aggregate([
      {
        $project: {
          _id: 0,
          year: {
            $year: "$date"
          },
          month: {
            $month: "$date"
          },
          description: "$description"
        }
      },
      {
        $match: {
          $and: [
            {
              description: {
                $in: [
                  "Baseball",
                  "Football",
                  "Swim"
                ]
              },
              year: 2021
            }
          ]
        }
      },
      {
        $group: {
          _id: {
            description: "$description",
            month: "$month"
          },
          count: {
            $sum: 1
          }
        }
      },
      {
        $group: {
          _id: {
            month_number: "$_id.month"
          },
          results: {
            $push: {
              description: "$_id.description",
              count: "$count"
            }
          }
        }
      },
      {
        $project: {
          _id: 0,
          month_number: "$_id.month_number",
          results: 1
        }
      }
    ])
    

    【讨论】:

    • 你的答案是正确的,但我不小心给了错误的数据,我用正确的数据更新了帖子
    • 好的,请立即查看mongoplayground.net/p/ZFR2NJ06Pj3
    • 有选项可以包含在不存在的结果计数字段中。例如,如果没有主题为“游泳”的数据,则给定月份 (2,7) 的计数为零
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    • 2011-02-25
    • 2021-01-16
    • 1970-01-01
    相关资源
    最近更新 更多