MongoDB-在&&组和项目内进行分组

时间:2019-12-17 03:18:08

标签: laravel mongodb mongodb-php

我是新手,正在与Laravel学习MongoDB。

这是我的问题。

我需要在60天之内获取记录,并按类型对其进行分组。

在每个类型组中,我必须按范围时间对它们进行分组:

  • 60-30天前
  • 30天前-现在

我研究了两天,到目前为止,这是我的密码。

$collection->aggregate([
    [
        '$match' => $match,
    ],
    [
        '$project' => [
            'range' => [
                '$concat' => [
                    [
                        '$cond' => [
                            [
                                '$and' => [
                                    ['$gte' => ['$created_at', $ago_60_days]],
                                    ['$lte' => ['$created_at', $ago_30_days]],
                                ],
                            ],
                            'before',
                            '',
                        ],
                    ],
                    [
                        '$cond' => [
                            [
                                '$and' => [
                                    ['$gt' => ['$created_at', $ago_30_days]],
                                    ['$lte' => ['$created_at', $now]],
                                ],
                            ],
                            'current',
                            '',
                        ],
                    ],
                ],
            ],
        ],
    ],
    [
        '$group' => [
            '_id' => '$type,
            'total' => ['$sum' => 1],
        ],
    ],
    ... anything after ???
]);

请让我知道任何事情,任何线索都可以帮助我。

我可以用 mongo shell 处理。

非常感谢你们。

1 个答案:

答案 0 :(得分:0)

查询:

db.test.aggregate( [
  { 
      $match: { 
          $expr: { 
              $gte:  [ "$dt", { $subtract: [ ISODate(), 60*24*60*60*1000 ] } ] 
          } 
      } 
  },
  { 
      $addFields: { 
          rangeTime: { 
              $cond: { 
                  if: { $gte: [ "$dt", { $subtract: [ ISODate(), 30*24*60*60*1000 ] } ] },
                  then: "range_30_now",
                  else: "range_60_30"
              } 
          } 
      }
  },
  {
      $group: { 
          _id: {
              t: "$type", r: "$rangeTime" 
          }, 
          total: { $sum: 1 } 
      }
  },
  { 
      $project: { 
          type: "$_id.t", 
          rangeTime: "$_id.r", 
          total: 1, 
          _id: 0 
      } 
  },
  { 
      $sort: { 
          type: 1, 
          rangeTime: -1  
      } 
  }
] )

输出:

{ "total" : 2, "type" : "A", "rangeTime" : "range_60_30" }
{ "total" : 1, "type" : "B", "rangeTime" : "range_60_30" }
{ "total" : 2, "type" : "B", "rangeTime" : "range_30_now" }

输入文档:

{ "_id" : 1, "dt" : ISODate("2019-10-15T00:00:00Z"), "type" : "A" }
{ "_id" : 2, "dt" : ISODate("2019-10-20T00:00:00Z"), "type" : "B" }
{ "_id" : 3, "dt" : ISODate("2019-11-08T00:00:00Z"), "type" : "A" }
{ "_id" : 4, "dt" : ISODate("2019-11-29T00:00:00Z"), "type" : "B" }
{ "_id" : 5, "dt" : ISODate("2019-11-15T00:00:00Z"), "type" : "A" }
{ "_id" : 9, "dt" : ISODate("2019-12-15T00:00:00Z"), "type" : "B" }