Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel MongoDB-集团内部集团和&;小组和项目_Laravel_Mongodb_Mongodb Php - Fatal编程技术网

Laravel MongoDB-集团内部集团和&;小组和项目

Laravel MongoDB-集团内部集团和&;小组和项目,laravel,mongodb,mongodb-php,Laravel,Mongodb,Mongodb Php,我是一个新手,正在和拉威尔一起学习MongoDB 这是我的问题 我需要在60天内获取记录,并按类型对其进行分组 在每个类型中组中,我必须按范围时间对它们进行分组: 60-30天前 30天前-现在 我用了两天的时间进行了研究,到目前为止,这些都是我的代码 $collection->aggregate([ [ '$match' => $match, ], [ '$project' => [ 'ran

我是一个新手,正在和拉威尔一起学习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

非常感谢各位。

查询:

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" }
查询:

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" }

如果我发布mongo shell(而不是laravel)的聚合代码,你能管理吗?@prasad_uuu是的。我能应付。请告诉我你的决定。谢谢。
type
是文档中的字段还是与“范围时间”相同?(我假设它是一个不同的字段)。如果我发布mongo shell(而不是laravel)的聚合代码,你能管理吗?@prasad_uuu是的。我能应付。请告诉我你的决定。谢谢。
type
是文档中的字段还是与“范围时间”相同?(我假设这是一个不同的领域)非常感谢。我正在尝试你的决心。非常感谢你。我在考验你的决心。