Mongo mapper use aggregation framework

41 views
Skip to first unread message

Muhammad Shahzad

unread,
May 8, 2017, 3:07:25 AM5/8/17
to f3-fra...@googlegroups.com

Hi,

Can someone please give me a example how I can sum a field using user id field?
https://fatfreeframework.com/3.6/mongo-mapper


Here is my code, this is returning me all documents matched with userId but not the sum.


 $f3 = \Base::instance();
$mapper
= new \DB\Mongo\Mapper($f3->get('MongoDB'),'transactions');
        $filter
= array('userId'=>$transArray['userId']);
        $options
= array(
                  array
(
                   
'group' => array(
                       
'_id' => array('userId' => $userId),
                       
'systemGeneratedCashback' => array('$sum' => 'systemGeneratedCashback')
                   
)
                 
)
       
);        
       $data
=  $mapper->find($filter, $options);

       echo
"<pre>";
       print_r
($data);
       
exit;



ikkez

unread,
May 10, 2017, 4:07:46 AM5/10/17
to Fat-Free Framework
maybe try it with map/reduce:

$options = array(
 
'group => array(
      '
keys' => array('userId' => 1),
      '
reduce' => 'function (obj, prev) {cash_sum += obj.systemGeneratedCashback;}',
      '
initial' => array('cash_sum' => 0),
      '
finalize' => '',
  );
)
Reply all
Reply to author
Forward
0 new messages