Taking sum of “column” in MongoDB

后端 未结 7 537

In MySQL, I would simply go \"SELECT sum(pts) FROM table\" to get the sum of the pts column on the table. However, I am converting this application to MongoDB and cannot fin

7条回答
  •  余生分开走
    2020-12-10 19:43

    Try:
    $m = new MongoClient();
    $con = $m->selectDB("dbName")->selectCollection("collectionName");
    $cond = array(
        array(
            '$group' => array(
                '_id' => null,
               'total' => array('$sum' => '$type'),
            ),
        )
    );
    
    $out = $con->aggregate($cond);
    print_r($out);
    

    More detail click here

提交回复
热议问题