Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb aggregation php

I'm using mongodb 2.1 and how to translate this query into php

db.counter.aggregate([ 
 { $match:{ page_id:123456 }}, 
 { $group:{_id:"$page_id",total:{$sum:"$pageview"}} } 
 ]);

Thanks

like image 758
user1457750 Avatar asked Dec 04 '22 15:12

user1457750


1 Answers

You can use the 'command()' method in PHP to run the aggregation framework as a database command. The precise syntax for your sample query would be:

   $conn = new Mongo("localhost:$port");
   $db = $conn->test;

   $result = $db->command (
            array( 
                "aggregate" => "counter",
                "pipeline" => 
                    array( 
                        array( '$match' => array( 'page_id' => 123456 )),
                        array( '$group' => array( "_id" => '$page_id',
                                    'total' => array( '$sum' => '$pageview')  
                                )
                            )
                    )
            )
        );
like image 134
William Z Avatar answered Dec 14 '22 21:12

William Z