【问题标题】:How to get sum of counted records using group by in mongodb?如何在 mongodb 中使用 group by 获取计数记录的总和?
【发布时间】:2018-12-12 10:21:28
【问题描述】:

我正在尝试从组中获得计数的总和,匹配。我怎么能得到相同的。 我有这个代码...

VisitorCompany.aggregate(
      [   
        {
          $match: {
            $and:[
              { entry_date: { $gt: start, $lt: end } }
            ]
          }
        },
          {
              $group:
              {
                  _id:
                  {
                      day: { $dayOfMonth: "$entry_date" },
                      month: { $month: "$entry_date" }, 
                      year: { $year: "$entry_date" }
                  }, 
                  count: { $sum:1 },
                  entry_date: { $first: "$entry_date" }
              }
          },
          {
              $project:
              {
                  entry_date:
                  {
                      $dateToString: { format: "%Y-%m-%d", date: "$entry_date" }
                  },
                  count: 1,
                  _id: 0
              }
          },
          { $sort : { entry_date : -1 } },
      ])

输出是...

        {
            "count": 2,
            "entry_date": "2018-12-12"
        },
        {
            "count": 1
            "entry_date": "2018-12-11"
        }

有没有人知道如何获得计数的总和,即 3 (2+1),表示组之前的记录总数。提前致谢。

【问题讨论】:

  • 请将示例数据以 JSON 格式发布
  • @ManjeetThakur,我认为没有必要,但这里是示例
  • { "_id": { "$oid": "xxxxxxxxx" }, "company_id": "xxxxxxxxxxxxx", "ip_address": "103.231.89.108", "country": "Australia", “大陆”:“OC”,“visit_date”:“2018-12-12”,“entry_date”:{“$date”:“2018-12-12T09:05:03.996Z”},“__v”:0}
  • 请检查日期约束 ` { $group: { _id: { day: { $dayOfMonth: "$entry_date" }, month: { $month: "$entry_date" }, year: { $年份:“$entry_date”}},计数:{$sum:1},entry_date:{$first:“$entry_date”}}},`
  • @ManjeetThakur,我想得到计数的总和,我只得到结果或获取的记录总数。查看输出

标签: mongodb mongodb-query aggregate


【解决方案1】:

您的以下修改后的查询将为您提供计数的总和,我刚刚添加了

$group:{_id:"", sum:{$sum: "$count"}}}

到现有的聚合管道查询

修改后的查询

VisitorCompany.aggregate(
      [   
        {
          $match: {
            $and:[
              { entry_date: { $gt: start, $lt: end } }
            ]
          }
        },
          {
              $group:
              {
                  _id:
                  {
                      day: { $dayOfMonth: "$entry_date" },
                      month: { $month: "$entry_date" }, 
                      year: { $year: "$entry_date" }
                  }, 
                  count: { $sum:1 },
                  entry_date: { $first: "$entry_date" }
              }
          },
          {
              $project:
              {
                  entry_date:
                  {
                      $dateToString: { format: "%Y-%m-%d", date: "$entry_date" }
                  },
                  count: 1,
                  _id: 0
              }
          },
          { $sort : { entry_date : -1 } },
          {$group:{_id:"", sum:{$sum: "$count"}}}
      ])

结果

{ "_id" : "", "sum" : 3 }

【讨论】:

  • 我尝试了相同的方法,但它改变了输出,如下所示,{ "count": 2, "entry_date": "2018-12-13" }, { "count": 4, "entry_date": "2018-12-12" } 它给出 { "_id": "", "sum": 6 } 我想要 { "_id": "", "sum": 6 } , { "count ": 2, "entry_date": "2018-12-13" }, {"count": 4, "entry_date": "2018-12-12" } 就像响应中的两者一样,如果不可能,则与另一个合计输出,所以我会得到它并添加响应
  • @RiteshDoomra - 请在问题部分从您的收藏中添加一些示例文档。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-27
相关资源
最近更新 更多