1
 db.tickers.aggregate(
{ $project: 
    {_id: 0,
    year: {$year: '$date'},
    month: {$month: '$date'},
    day: {$dayOfMonth: '$date'},
    hour: {$hour: '$date'},
    avg: '$ticker.avg'     
    }
},
{ $group: { 
    _id: { year: '$year', month: '$month', day: '$day', hour: '$hour' },
    avg: { $avg: '$avg' }
    }
},
{ $sort: {
    'year':1, month:1, day:1, hour:1
    }
}
);

你如何在 Lithium 中编写上述查询?

我试过了:

        $mongodb = Connections::get('default')->connection;
        $tick = Tickers::connection()->connection->command(array(
          'aggregate' => 'tickers',
          'pipeline' => array( 
                            '_id'=>null,
                            array('year' => array('$year' => '$date')),
                            array('month' => array('$month' => '$date')),                               
                            array('day' => array('$dayOfMonth' => '$date')),                                
                            array('hour' => array('$hour' => '$date')),                             
                            array('avg' => '$ticker.avg'),                              
                            ),
                            array( '$group' => array( '_id' => array(
                                    'year'=>'$year',
                                    'month'=>'$month',
                                    'day'=>'$day',
                                    'hour'=>'$hour'
                                    ),
                                'avg' => array('$avg' => '$ticker.avg'),  
                            ),
                            array('$sort'=>array(
                                'year'=>1,
                                'month'=>1,
                                'day'=>1,
                                'hour'=>1
                            ))
                        )
        ));

这是对问题的回复,我有mongodb:find summary of records

find-summary of records 问题的答案是正确的,但我不能将它放在 Lithium 中。

提前致谢 :)

4

1 回答 1

2

您忘记了$project对象的级别。试试这个:

    $tick = Tickers::connection()->connection->command(array(
      'aggregate' => 'tickers',
      'pipeline' => array( 
                        array( '$project' => array(
                            '_id' => 0,
                            'year' => array('$year' => '$date'),
                            'month' => array('$month' => '$date'),                               
                            'day' => array('$dayOfMonth' => '$date'),                                
                            'hour' => array('$hour' => '$date'),                             
                            'avg' => '$ticker.avg',                              
                        )),
                        array( '$group' => array( '_id' => array(
                                'year'=>'$year',
                                'month'=>'$month',
                                'day'=>'$day',
                                'hour'=>'$hour'
                                ),
                            'avg' => array('$avg' => '$ticker.avg'),  
                        )),
                        array('$sort'=>array(
                            'year'=>1,
                            'month'=>1,
                            'day'=>1,
                            'hour'=>1
                        ))
                    )
    ));
于 2013-01-02T15:42:49.757 回答