MongoDB聚合PHP,按小时分组 [英] MongoDB Aggregation PHP, Group by Hours

查看:57
本文介绍了MongoDB聚合PHP,按小时分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下格式的文件

{
  "_id" : ObjectId("12e123123123123123"),
  "client_id" : "12345667889",
  "resource" : "test/test",
  "version" : "v2",
  "ts" : new Date("Wed, 02 Jan 2013 15:34:58 GMT +08:00")
}

ts是MongoDate()字段.

The ts is a MongoDate() field.

我正在尝试使用php中的MongoDB聚合函数按client_id和hour分组,并统计使用量以显示在表/图中

I am trying to use MongoDB aggregate function in php to group by client_id and hour and count usage to display in a table/graph

这是我目前的尝试

$usage = $this->db->Usage->aggregate(array(array(
  '$project' => array(
    'hour' => array(
      'years' => array( '$year' => '$ts' ),
      'months' => array( '$month' => '$ts' ),
      'days' => array( '$dayOfMonth' => '$ts' ),
      'hours' => array( '$hour' => '$ts' ),
    )
  ),
  '$group' => array(
    '_id' => array(
      'client_id' => '$client_id',
      'hour' => '$hour',
    ),
   'number' => array('$sum' => 1),
  )
)));

不幸的是,我回来的答复只是按client_id分组,仅此而已.

Unfortunately my response I am getting back is just grouping by the client_id and nothing else.

[result] => Array
    (
        [0] => Array
            (
                [_id] => Array
                    (
                        [client_id] => adacf8deba7066e4
                    )

                [number] => 12
            )

    )

有人能指出我正确的方向,还是有其他解决办法按小时分组?

Anyone able to point me in the right direction, or have another solution to group by hour?

-固定- PHP要求您将每个管道放置在单独的数组中,并将整个对象放置在一个数组中.请在JohnnyHK的帮助下查看下面的更新解决方案

--Fixed-- PHP requires that you put each pipeline in a separate array as well as putting the whole thing in an array. see update solution below with help from JohnnyHK

$usage = $this->db->Usage->aggregate(array(array(
        '$project' => array(
            'client_id' => 1,
            'hour' => array(
                'years' => array( '$year' => '$ts' ),
                'months' => array( '$month' => '$ts' ),
                'days' => array( '$dayOfMonth' => '$ts' ),
                'hours' => array( '$hour' => '$ts' ),
            )
        )),array(
        '$group' => array(
            '_id' => array(
                'client_id' => '$client_id',
                'hour' => '$hour',
            ),
            'number' => array('$sum' => 1),
        )
    )));

推荐答案

它不应创建您看到的结果,但您确实需要在$project运算符中包含client_id,以便它可用于$group运算符.

It shouldn't create the results you're seeing, but you do need to include client_id in your $project operator so that it's available to the $group operator.

'$project' => array(
  'client_id' => 1,
  'hour' => array(
    'years' => array( '$year' => '$ts' ),
    'months' => array( '$month' => '$ts' ),
    'days' => array( '$dayOfMonth' => '$ts' ),
    'hours' => array( '$hour' => '$ts' ),
  )
),

在进行了此更改之后,等效的shell起作用了:

The shell equivalent worked after I made that change:

db.test.aggregate(
    { $project: {
        hour: {
            years: {$year: '$ts'},
            months: {$month: '$ts'},
            days: {$dayOfMonth: '$ts'},
            hours: {$hour: '$ts'}
        },
        client_id: '$client_id'
    }},
    { $group: {
        _id: { client_id: '$client_id', hour: '$hour' },
        number: { $sum: 1}
    }})

返回:

{
  "result": [
    {
      "_id": {
        "client_id": "12345667889",
        "hour": {
          "years": 2013,
          "months": 1,
          "days": 2,
          "hours": 7
        }
      },
      "number": 1
    }
  ],
  "ok": 1
}

这篇关于MongoDB聚合PHP,按小时分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆