【问题标题】:Group and count distinct occurrences对不同的事件进行分组和计数
【发布时间】:2017-06-02 17:26:36
【问题描述】:

我正在尝试派生一个查询以获取不同值的计数并显示相关字段。分组由tempIddate 完成,其中tempId 可以在一天内和一个时间范围内出现one-to-many 次。

以下是我的方法,

db.getCollection('targetCollection').aggregate(    
{    
   $match:{    
       "user.vendor": 'vendor1',     
       tool: "tool1",     
       date: {    
           "$gte": ISODate("2016-04-01"),    
           "$lt": ISODate("2016-04-04")    
       }    
    }    
},     
{    
   $group:{    
       _id: {     
           tempId: '$tempId',
           month: { $month: "$date" },     
           day: { $dayOfMonth: "$date" },     
           year: { $year: "$date" }     
       },    
       count: {$sum : 1}    
    }     
},
{    
   $group:{    
       _id: 1,    

       count: {$sum : 1}    
    }     
})

此查询生成以下输出,

{
    "_id" : 1,
    "count" : 107
}

这是正确的,但我想将它们按日期分开,并带有该日期的特定计数。比如这样的,

{
    "date" : 2016-04-01
    "count" : 50
},
    {
    "date" : 2016-04-02
    "count" : 30
},
    {
    "date" : 2016-04-03
    "count" : 27
}

附:我不确定如何将这个问题放在一起,因为我对这项技术很陌生。如果问题需要改进,请告诉我。

以下是我尝试查询的 mongodb 集合的示例数据,

{
    "_id" : 1,
    "tempId" : "temp1",
    "user" : {
        "_id" : "user1",
        "email" : "user1@email.com",
        "vendor" : "vendor1"
    },
    "tool" : "tool1",
    "date" : ISODate("2016-03-09T08:30:42.403Z")
},...

【问题讨论】:

    标签: mongodb distinct aggregation-framework robo3t


    【解决方案1】:

    我自己想出了解决方案。我所做的是,

    • 我首先按tempIddate 分组
    • 然后我按date分组

    这打印出tempId 的每日不同计数,这是我想要的结果。查询如下,

    db.getCollection('targetCollection').aggregate(    
    {    
       $match:{    
           "user.vendor": 'vendor1',     
           tool: "tool1",     
           date: {    
               "$gte": ISODate("2016-04-01"),    
               "$lt": ISODate("2016-04-13")    
           }    
        }    
    },     
    {    
       $group:{    
           _id: {     
               tempId: "$tempId",
               month: { $month: "$date" },     
               day: { $dayOfMonth: "$date" },     
               year: { $year: "$date" }     
           },    
           count: {$sum : 1}    
        }     
    },
    {    
       $group:{    
           _id: {     
               month:"$_id.month" ,     
               day: "$_id.day" ,     
               year: "$_id.year"     
           },    
           count: {$sum : 1}    
        }     
    })
    

    【讨论】:

      【解决方案2】:

      通过日期将它们分组

      db.getCollection('targetCollection').aggregate([
          {
             $match:{    
                 "user.vendor": 'vendor1',     
                 tool: "tool1",     
                 date: {    
                     "$gte": ISODate("2016-04-01"),    
                     "$lt": ISODate("2016-04-04")    
                 }    
              }    
          },
          {
              $group: {
                  _id: {
                      date: "$date",
                      tempId: "$tempId"
                  },
                  count: { $sum: 1 }
              }
          }
      ]);
      

      【讨论】:

      • 感谢您的回答,但需要考虑tempId。我试图得出的查询是获取给定日期范围内每一天的每个 distinct tempIdcount。我已经更新了这个问题。 :)
      • 嘿,谢谢,但我认为它更接近但它和我一样。日期应该像我所做的那样完成,因为时间戳包含秒数,因此分组无论如何都会给出一个实例。我需要每天获取 distinct tempIds 的 total count
      猜你喜欢
      • 2021-01-23
      • 1970-01-01
      • 1970-01-01
      • 2016-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多