Mongodb php aggregation

I am using mongodb 2.1 and how to translate this request in php

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

thanks

+4
source share
2 answers

You can use the "command ()" method in PHP to start the aggregation structure as a database command. The exact syntax for your sample request is:

  $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') ) ) ) ) ); 
+15
source

Try:

 $m = new MongoClient(); $con = $m->selectDB("dbName")->selectCollection("counter"); $cond = array( array('$match' => array('page_id' =>123456)), array( '$group' => array( '_id' => '$page_id', 'total' => array('$sum' => '$pageview'), ), ) ); $out = $con->aggregate($cond); print_r($out); 

Read more ... click here

0
source

All Articles