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

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();

In my case, match() was somewhat tricky on a MongoId object.

This didn't work for me:

$userMongoId = "57321c7a2977f8de306ef648";
$aq ->match([ 'user.$id' => $expr->eq($userMongoId) ])

This worked:

$aq ->match([ 'user' => new \MongoId($userMongoId) ])

Tested on Symfony 3 with the SolutionMongoAggregationBundle, MongoDB version 3.2.6.