4

我正在使用 mongodb 2.1 以及如何将此查询转换为 php

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

谢谢

4

2 回答 2

16

您可以使用 PHP 中的“command()”方法将聚合框架作为数据库命令运行。您的示例查询的精确语法是:

   $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')  
                                )
                            )
                    )
            )
        );
于 2012-07-03T17:28:46.913 回答
0

尝试:

$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);

更多详情请点击这里

于 2017-04-11T13:31:59.463 回答