Shanty Mongo汇总$ multiply

时间:2016-01-05 17:59:58

标签: php mongodb aggregation-framework

如何从MongoDB聚合中的组管道获取桌面和移动设备之间的百分比。

示例数据:

{
   _id : 1,
   full_url : 'www.example.com/blog/about',
   access_time : ISODate("2015-12-21T14:50:19Z")
},
{
   _id : 2,
   full_url : 'www.example.com/blog/statistic',
   access_time : ISODate("2015-12-21T14:50:22Z")
},
{
   _id : 3,
   full_url : 'm.example.com/detail/article1',
   access_time : ISODate("2015-12-21T14:50:41Z")
}

结果应如下所示:

{
   _id : 'www.example.com',
   percentage : 28
},
{
   _id : 'm.example.com',
   percentage : 72
}

Shanty Mongo

class App_Model_Mongodb_RequestLog extends Shanty_Mongo_Document
{
    protected static $_db = 'sam';
    protected static $_collection = 'requestlog';

    public static function device()
    {
        $total = self::all()->count();
        $pipeline = [
            [
               '$match' => [
                    'access_time' => [
                         '$gte' => new \MongoDate( strtotime('-1 minute') ),
                         '$lte' => new \MongoDate(),
                     ],
                     '$and' => [
                         ['full_url' => new \MongoRegex("/www.example.com/i")],
                         ['full_url' => new \MongoRegex("/m.example.com/i")],
                      ]
               ]
            ],
            [
                '$group' => [
                       '_id' => '$full_url',
                       'count' => ['$sum' => 1]
                 ]
            ],
            [
                 '$project' => [
                       'percentage' => [
                                '$multiply' => [
                                        '$count', 100 / $total
                                 ]
                        ]
                 ]
            ],
            [
                 '$sort' => ['percentage' => -1]
            ]
        ];
        return self::getMongoCollection()->aggregate($pipeline);
    }
}

但我不知道该怎么做。我尝试过$ divide和其他东西,但没有成功。有什么建议吗?

0 个答案:

没有答案