dsfgds4215 2016-04-25 13:18
浏览 49
已采纳

Symfony mongo-odm-aggregation-bundle sums

For start, I have to say I am new to mongo (3.2). I am using mongo-odm-aggregation-bundle for php framework Symfony (2.8.4). I want to get sums of some fields restricted by dates. So far, I managed to get sums for all records:

$expr = new \Solution\MongoAggregation\Pipeline\Operators\Expr;
        $aq = $this->manager->getCollection('AppBundle:UserDaySums')->createAggregateQuery()->group([
                            '_id' => 'client.$id',
                            'total' => $expr->sum('$aSum'),
                        ])

Now, I'd like to restrict this query by dateFrom,dateTo and userId. I am not sure, how to do it. I know, I should probably use match function, but I don't know how. Or is there some better solution?

Thanks for replies!

KP

  • 写回答

2条回答 默认 最新

  • douren6874 2016-04-25 15:03
    关注

    Yes, you can use the match function. For example, the following assumes you have the date variables for use in the query:

    $expr = new \Solution\MongoAggregation\Pipeline\Operators\Expr;
    $aq = $this->manager->getCollection('AppBundle:UserDaySums')->createAggregateQuery();
    $dateFrom = new MongoDate(strtotime('-2 days'));
    $dateTo = new MongoDate();
    $userId = 'Xsgy62js0lb';
    
    $result = $aq->match(['dateFrom'=>$dateFrom, 'dateTo'=>$dateTo, 'userId'=>$userId ])
                 ->group(['_id'=>'client.$id', 'total'=>$expr->sum('$aSum') ])
                 ->getQuery()->aggregate();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答