2

I am confused to ask this question but I can not find a solution to my problem. I use the mongo-odm-agregation-bundle to perform an aggregate on my data.

I don't know how to use correctly this bundle, the documentation is not sufficiently explicit and the result is not that i would expect.

So, in mongoDB my code is for the aggregate :

id: { Epreuve:"$EPREUVE", month: { $month: "$DATE" }, 
      day: { $dayOfMonth: "$DATE" }, year: { $year: "$DATE" }}, 
total: { $sum: "$SCORE" },
nbmots: {$sum: "$NBMOTS"},
moymots: {$avg : "$NBMOTS"},
moytemps:{$avg: "$CHRONOS"},
position: { $sum: 1 },

And the result is :

{ 
    "_id" : {
        "Epreuve" : "Verbe", 
        "month" : NumberInt(2), 
        "day" : NumberInt(21), 
        "year" : NumberInt(2017)
    }, 
    "total" : NumberLong(430), 
    "nbmots" : NumberLong(16), 
    "moymots" : 16.0, 
    "moytemps" : 147.24, 
    "position" : 1.0
}

In Symfony, i use this sample to test :

$expr = new \Solution\MongoAggregation\Pipeline\Operators\Expr;
$aq = $this->get('doctrine_mongodb.odm.default_aggregation_query')->getCollection('PortailBundle:DataScoreMotsMeles')->createAggregateQuery();
        $result = $aq->match(['SESSION'=>$currentSession])
                 ->group(['_id' => [ 'Epreuve' => "EPREUVE", ],
                          'Score' => $expr->sum("SCORE"),
                          'nbMots'=> $expr->sum("NBMOTS"),
                          'moyMots'=> $expr->avg("NBMOTS"),
                          'moyTemps'=> $expr->avg("CHRONOS"),        
                          'count' => $expr->sum(1)])
                   ->sort(['count' => -1])
                   ->limit(10)
                   ->getQuery()
                   ->aggregate()
                   ->toArray();

The result is :

Array ( [0] => Array ( [_id] => Array ( [Epreuve] => **EPREUVE** ) [Score] => **0** [nbMots] => **0** [moyMots] => [moyTemps] => [count] => 3 ) ) 

The problem is the result is 0 each time.

It is normal because i use :

$expr->sum("NBMOTS")

instead of :

$expr->sum('$NBMOTS') 

But if i use '$NBMOTS' it doesn't works. So how i do ? I need your help.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317

0 Answers0