2012-10-03 41 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