duanbu9345 2013-07-19 03:55
浏览 140

SUM()在Mongodb? 查询问题?

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

  • 写回答

2条回答 默认 最新

  • dsbtwy1329 2013-07-19 04:33
    关注

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

    Mongocollection Aggregate

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度