duanan6043 2015-02-11 12:20
浏览 254
已采纳

PHP和MongoDB显示按日期分组的结果

I have an mongodb collection with following documents:

{
  "_id" : ObjectId("547af6aea3f0eba7148b4567"),
  "check_id" : "f5d654e7-257d-4a93-ae50-2d59dfeeb451",
  "chunks" : NumberLong(200),
  "num_hosts" : NumberLong(1000),
  "num_rbls" : NumberLong(163),
  "owner" : NumberLong(7901),
  "created" : ISODate("2014-11-30T10:51:26.924Z"),
  "started" : ISODate("2014-11-30T10:51:31.558Z"),
  "finished" : ISODate("2014-11-30T10:57:08.512Z")
}

{
  "_id" : ObjectId("54db19a858a5d395a18b4567"),
  "check_id" : "9660e510-1349-43f3-9d5e-8bf4b06179be",
  "chunks" : NumberLong(2),
  "num_hosts" : NumberLong(10),
  "num_rbls" : NumberLong(166),
  "owner" : NumberLong(7901),
  "created" : ISODate("2015-02-11T08:58:17.118Z"),
  "started" : ISODate("2015-02-11T08:58:18.78Z"),
  "finished" : ISODate("2015-02-11T08:58:47.486Z")
}


{
  "_id" : ObjectId("54db267758a5d30eab8b4567"),
  "check_id" : "9660e510-1349-43f3-9d5e-8bf4b06179be",
  "chunks" : NumberLong(2),
  "num_hosts" : NumberLong(10),
  "num_rbls" : NumberLong(166),
  "owner" : NumberLong(7901),
  "created" : ISODate("2015-02-11T09:52:55.388Z"),
  "started" : ISODate("2015-02-11T09:52:56.109Z"),
  "finished" : ISODate("2015-02-11T09:53:22.095Z")
}

What I need is to get the result and produce an array similar to this:

Array
(
    [2015-02-11] => array
        (
            //array with results from 2015-02-11
        )
    [2014-11-30] => array
        (
            //array with results from 2014-11-30
        )
)

I know that it's possible to just perform simply collection->find and then loop through results and use php logic to achieve my goal but is it possible to make it using mongo? Maybe using aggregation framework?

EDIT: I want to group results by "created" date

Any help will be highly appreciated.

  • 写回答

2条回答 默认 最新

  • dongna1593 2015-02-11 13:34
    关注

    I have managed to solve this using the aggregation framework. Here is the answer, in case anyone need it.

    $op = array(
        array(
            '$project' => array(
                'data' => array(
                    'check_id' => '$check_id',
                    'chunks' => '$chunks',
                    'num_hosts' => '$num_hosts',
                    'num_rbls' => '$num_rbls',
                    'owner' => '$owner',
                    'started' => '$started',
                    'finished' => '$finished',
                ),
                'year' => array('$year' => '$created' ),
                'month' => array('$month' => '$created' ),
                'day' => array('$dayOfMonth' => '$created'),
            )
        ),
        array(
            '$group' => array(
                '_id' => array('year' => '$year', 'month' => '$month', 'day' => '$day'),
                'reports_data' => array('$push' => '$data'),
            )
        ),
    );
    
    $c = $collection->aggregate($op);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!