2012-10-03 37 views
0

我有一个名为“通知”的mongodb集合,我想要的是,查询所有通知并按“日期”对它们进行分组。我有一个名为“date_created”的具有MongoDate()对象的通知集合中的字段,所以我想要的输出是MongoDB群由日期

通过按=>“今日”分组的通知,其中包含今天创建的所有通知 通过按=>“昨天”分组的通知,其中包含昨天创建的所有通知,

并且该列表每天都会发布。

回答

0
 $pipeline= array('$group' => array("_id" => '$col2', 
                  'col1' => array('$sum' => '$col1') , 
                  'col3' => array('$sum' => '$col3'), 
                   'col4' => array('$sum' => '$col4')  
                 ) 
                ) ;    

        if ($this -> publisher_id) {   
         $pipeline=array_merge($pipeline, array('col5' => $this -> col5value)); 
        } 

        $conn = new Mongo(); 
        $c = $conn->$db; 

        $result = $c-> command (
            array( 
             "aggregate" => "tablename", 
             "pipeline" => 
              array($pipeline) 
            ) 
           ); 
Note : sql query : 
select sum(col1) as col1,... from tablename 
where col5='value' group by col2 
0

您的分组需要多复杂?您可能只想在文档的相应字段上使用sort