按字段 1 聚合组并计算字段 2 的总和

Posted

技术标签:

【中文标题】按字段 1 聚合组并计算字段 2 的总和【英文标题】:Aggregate Group by Field 1 and calculate the sum of Field 2 【发布时间】:2021-04-19 13:20:02 【问题描述】:

我有这个猫鼬查询:

const getMinutesSpentStudyingByUserById = function getMinutesSpentStudyingByUserById(
  userId
) 
  const minutesSpentStudyingWithUserPromise = MinutesSpentStudying.aggregate([
     $match:  connected_user_id: ObjectId(userId)  ,
    
      $project: 
        minutes_spent_studying: 1,
        week:  $week: "$date" ,
      ,
    ,
  ]);
  return minutesSpentStudyingWithUserPromise;
;

它返回这个:

 [
    
        "_id": "5fe765765327cc0017c924cc",
        "minutes_spent_studying": 1,
        "week": 51
    ,
    
        "_id": "5fe8461f247ff100177fde30",
        "minutes_spent_studying": 1,
        "week": 52
    ,
    
        "_id": "5fe8467e247ff100177fde31",
        "minutes_spent_studying": 1,
        "week": 52
    ,
    
        "_id": "5fe85a1d2eca030017c894bf",
        "minutes_spent_studying": 1,
        "week": 52
    ,
    
        "_id": "5fe85a212eca030017c894c0",
        "minutes_spent_studying": 0,
        "week": 52
    ,
    
        "_id": "5fe85a222eca030017c894c1",
        "minutes_spent_studying": 0,
        "week": 52
    ,
    
        "_id": "5fe85a242eca030017c894c2",
        "minutes_spent_studying": 0,
        "week": 52
    ,
    
        "_id": "5feda157268ac9001796806e",
        "minutes_spent_studying": 1,
        "week": 52
    ,
    
        "_id": "5feda814f6729c001706460a",
        "minutes_spent_studying": 1,
        "week": 52
    ,
    
        "_id": "5fedb14d055a010017cfc1e7",
        "minutes_spent_studying": 1,
        "week": 52
    ,
    
        "_id": "5fedb38e055a010017cfc1e8",
        "minutes_spent_studying": 1,
        "week": 52
    ,
    
        "_id": "5fedb422055a010017cfc1e9",
        "minutes_spent_studying": 1,
        "week": 52
    ,
    
        "_id": "5ff023ed90d6ee0017adb9b7",
        "minutes_spent_studying": 1,
        "week": 0
    ,
    
        "_id": "5ff0244590d6ee0017adb9b8",
        "minutes_spent_studying": 1,
        "week": 0
    ,
    
        "_id": "5ff0271d90d6ee0017adb9b9",
        "minutes_spent_studying": 4,
        "week": 0
    ,
    
        "_id": "5ff028f190d6ee0017adb9ba",
        "minutes_spent_studying": 3,
        "week": 0
    ,
    
        "_id": "5ff029b090d6ee0017adb9bb",
        "minutes_spent_studying": 2,
        "week": 0
    ,
    
        "_id": "5ff16ffbb2e3980017fa8875",
        "minutes_spent_studying": 1,
        "week": 1
    ,
    
        "_id": "5ffec8aa7757ce00171a3b2d",
        "minutes_spent_studying": 1,
        "week": 2
    ,
    
        "_id": "5ffec92f7757ce00171a3b2e",
        "minutes_spent_studying": 1,
        "week": 2
    ,
    
        "_id": "5ffeca267757ce00171a3b2f",
        "minutes_spent_studying": 1,
        "week": 2
    ,
    
        "_id": "5ffeca797757ce00171a3b30",
        "minutes_spent_studying": 1,
        "week": 2
    ,
    
        "_id": "5ffed5cc7757ce00171a3b32",
        "minutes_spent_studying": 1,
        "week": 2
    ,
    
        "_id": "5ffed6587757ce00171a3b33",
        "minutes_spent_studying": 1,
        "week": 2
    ,
    
        "_id": "5ffed6a57757ce00171a3b34",
        "minutes_spent_studying": 1,
        "week": 2
    ,
    
        "_id": "5ffeeab7028e53568243c5f0",
        "minutes_spent_studying": 2,
        "week": 2
    ,
    
        "_id": "5ffeeb3b028e53568243c5f1",
        "minutes_spent_studying": 1,
        "week": 2
    ,
    
        "_id": "5ffeebc5028e53568243c5f2",
        "minutes_spent_studying": 2,
        "week": 2
    ,
    
        "_id": "5ffeec50028e53568243c5f3",
        "minutes_spent_studying": 1,
        "week": 2
    ,
    
        "_id": "5ffeecbb028e53568243c5f4",
        "minutes_spent_studying": 1,
        "week": 2
    ,
    
        "_id": "5fffffc6f8f7231671373796",
        "minutes_spent_studying": 3,
        "week": 2
    ,
    
        "_id": "6000013cf8f7231671373797",
        "minutes_spent_studying": 1,
        "week": 2
    ,
    
        "_id": "6000044eff6b480017ba1e42",
        "minutes_spent_studying": 1,
        "week": 2
    ,
    
        "_id": "600004bfff6b480017ba1e43",
        "minutes_spent_studying": 1,
        "week": 2
    ,
    
        "_id": "60003249417f7860848d8093",
        "minutes_spent_studying": 18,
        "week": 2
    
]

我想知道每周学习的分钟数。 所以我必须按周分组并按 minutes_spent_studying 计算总和

我设法按周分组,但我无法按分钟计算总和_spent_studying:

const getMinutesSpentStudyingByUserById = function getMinutesSpentStudyingByUserById(
  userId
) 
  const minutesSpentStudyingWithUserPromise = MinutesSpentStudying.aggregate([
     $match:  connected_user_id: ObjectId(userId)  ,
    
      $project: 
        minutes_spent_studying: 1,
        week:  $week: "$date" ,
      ,
    ,
    
      $group: 
        _id: "$week",
        count:  $sum: 1 ,
      ,
    ,
  ]);

  return minutesSpentStudyingWithUserPromise;
;

这导致了:

[
    
        "_id": 2,
        "count": 17
    ,
    
        "_id": 52,
        "count": 11
    ,
    
        "_id": 1,
        "count": 1
    ,
    
        "_id": 51,
        "count": 1
    ,
    
        "_id": 0,
        "count": 5
    
]

我找不到一种方法来计算每周的 minutes_spent_studying 总和。

【问题讨论】:

【参考方案1】:

您也可以使用 $sum 引用另一个字段,该字段将返回每个组的总分钟数,尝试:


    $group: 
        _id: "$week",
        count:  $sum: 1 ,
        total_minutes:  $sum: "$minutes_spent_studying" ,
    

Mongo Playground

【讨论】:

以上是关于按字段 1 聚合组并计算字段 2 的总和的主要内容,如果未能解决你的问题,请参考以下文章

上)

MongoDB 中的计算

mysql之聚合函数

如何对 Solr 中的多个字段执行嵌套聚合?

根据 BigQuery 重复记录中的字段计算聚合

MySQL数据操作与查询(第六章 上)