Question

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

How do you write the above query in Lithium?

I have tried:

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

This is reply to the question, I had mongodb: find summary of records

The answer to the question find-summary of records, is correct, but I cannot place it in Lithium.

Thanks in advance :)

Was it helpful?

Solution

You forgot the $project level of the object. Try this instead:

    $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
                        ))
                    )
    ));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top