mongodb php query in documents with nested objects -


so here sample of document in mongodb collection:

{     "_id" : objectid("561e0de61c9218b7bf9877c3"),     "date" : numberlong(20151014),     "hour" : numberlong(10),     "productid" : objectid("5614ba9c2e131caa098b4567"),     "productname" : "test",     "producerid" : objectid("5617802151f8adf4db329d52"),     "producername" : "producer",     "producerrate" : numberlong(300),     "producermedium" : "emailer",     "totallead" : numberlong(6),     "verifiedlead" : numberlong(3),     "unverifiedlead" : numberlong(2),     "unqualifiedlead" : numberlong(1),     "totalearning" : numberlong(660),     "consumers" : [          {             "consumerid" : objectid("5617802151f8adf4db329d54"),             "consumername" : "consumer1",             "consumedrate" : numberlong(120),             "consumedlead" : numberlong(3),             "earning" : numberlong(360)         },          {             "consumerid" : objectid("5617802151f8adf4db329d58"),             "consumername" : "consumer2",             "consumedrate" : numberlong(100),             "consumedlead" : numberlong(3),             "earning" : numberlong(300)         }     ] } 

now want consumedlead grouped consumerid , productid database in php.

what have did far give me totallead , verifiedlead grouped product id have no idea how consumerbased results same:

$keyf = new mongocode('function(doc) {                       return {\'productid\': doc.productid,\'productname\': doc.productname};         }');        $initial = array('totallead'=>0,'verifiedlead'=>0);          $reduce = "function(obj, prev) {         prev.totallead += obj.totallead;         prev.verifiedlead += obj.verifiedlead;          }";         $result = $collection->group($keyf, $initial, $reduce);          var_dump($result); 

any please.

edit: expected result wpuld :

{ [0]=> array(4) { ["productid"]=> object(mongoid)#8 (1) { ["$id"]=> string(24) "5614ba9c2e131caa098b4567" } ["productname"]=> string(4) "test" ["consumerid"]=> object(mongoid)#8 (1) { ["$id"]=> string(24) "5617802151f8adf4db329d58" } ["consumedlead"]=> float(4) } } 

the solution use aggregation framework operation includes $unwind operator initial pipeline stage deconstruct consumers array field input documents , outputs document each element. each output document replaces array element value. make possible $sum group accumulator operator in $group step work , givies required consumedlead grouped consumerid , productid:

db.collection.aggregate([     {         "$unwind": "$consumers"     },     {         "$group": {             "_id": {                 "productid": "$productid",                 "consumerid": "$consumers.consumerid"             },             "totalconsumedlead": {                 "$sum": "$consumers.consumedlead"             }         }     } ]) 

running aggregation operation on above sample result:

/* 0 */ {     "result" : [          {             "_id" : {                 "productid" : objectid("5614ba9c2e131caa098b4567"),                 "consumerid" : objectid("5617802151f8adf4db329d58")             },             "totalconsumedlead" : numberlong(3)         },          {             "_id" : {                 "productid" : objectid("5614ba9c2e131caa098b4567"),                 "consumerid" : objectid("5617802151f8adf4db329d54")             },             "totalconsumedlead" : numberlong(3)         }     ],     "ok" : 1 } 

so final working aggregation in php should be:

$pipeline = array(         array('$unwind' => '$consumers'),     array(         '$group' => array(             '_id' => array(                 'productid' => '$productid',                 'consumerid' => '$consumers.consumerid',             ),             'totalconsumedlead' => array(                 '$sum' => '$consumers.consumedlead'             ),         )     ), );  $out = $collection->aggregate($pipeline ,$options); 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -