SUM()在Mongodb?查询问题?

时间:2013-07-19 03:55:12

标签: php sql mongodb

我正在尝试从SQL编写一个PHP(Mongo)查询,其中我有SUM(),但我不确定我的语法是否正确。有人可以开导我吗?

SQL:

$cmd = "SELECT SUM(m_length) FROM pkt_tbl WHERE m_time>=" . $time. " AND m_buffer_latency<=" . $time;

Mongodb查询:

    $find_projection= aggregate(array('$group'=>array('$sum'=>'$m_length')));
    $result = $table -> command($find_projection);

我可以使用array_sum是$ result还是在这种情况下我可以使用$ SUM(Aggregate)。任何帮助将不胜感激。

由于

2 个答案:

答案 0 :(得分:3)

您应该尝试aggregation

php中,此代码可以为您提供帮助,

<?php
    $m = new Mongo;
    $c = $m->selectDB("test")->selectCollection("zips");

    $out = $c->aggregate(array(
            '$group' => array(
                '_id' => '$state',
               'totalPop' => array('$sum' => '$pop')
            )
        ),
        array(
            '$match' => array('totalPop' => array('$gte' => 10*1000*1000))
        )
    );

    var_dump($out);
?>

Mongocollection Aggregate

答案 1 :(得分:0)

$result = $table->aggregate(
array('$match' => array(
'm_time' => array('$gte' => $max),
'm_buffer_latency' => array('$lte' => $max),
)),
array('$group' => array(
'_id' => true,
'sum_length' => array('$sum' => '$m_length')
))
); 

这回答了所有问题