PHP7+mongoDB+aggregate

mongdb客户端操作:

db.article.aggregate([
    { $match:{pub_time:{$gte:1546819200,$lt:1546905600}}},
    {$project:{source:1,media:1}},
    {$group:{_id:{source:'$source',media:'$media'}, nums:{$sum:1}}}
]);

PHP7操作:



​$command = [
             'aggregate' => 'article',
             'pipeline' => [
                 ['$match' => [ 'pub_time' => ['$gte'=>1546819200, '$lt'=>1546905600] ]],
                 ['$group' => [ '_id' => ['source'=>'$source','media'=>'$media'], 'nums' => ['$sum'=>1] ] ],
                 ['$limit' => 10],
             ],
             'cursor' => new \stdClass,
        ];  
 
        $cursor = $mongo->command($command);
        foreach ($cursor as $document) {
             var_dump($document);
         }

PS:如果要更新某个字段嵌套的对象(fields数组中某一字段用field.key)

其中用到的PHP7+mongodb的类库参考:https://blog.csdn.net/why444216978/article/details/85329366

猜你喜欢

转载自blog.csdn.net/why444216978/article/details/86494442