I am trying to write a PHP (Mongo) query from SQL in which I have SUM() but I am not sure my syntax is correct. Can someone enlighten me?
SQL:
$cmd = "SELECT SUM(m_length) FROM pkt_tbl WHERE m_time>=" . $time. " AND m_buffer_latency<=" . $time;
Mongodb Query:
$find_projection= aggregate(array('$group'=>array('$sum'=>'$m_length')));
$result = $table -> command($find_projection);
Can I use array_sum is the $result or is there anyway I can use $SUM (Aggregate) in this case. Any help would be appreciated.
Thanks
You should try aggregation
In php
this code may help you,
<?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);
?>
$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')
))
);
This answers it all