mongodb: find summary of records

I have a collection called 'tickers'

Data is formatted as:

{
  "_id": ObjectId("50d192b5480cf44157000000"),
  "ticker": {
    "high": 13.38839,
    "low": 13.1225,
    "avg": 13.244705635,
    "vwap": 13.258414151,
    "vol": NumberInt(21562),
    "last_all": 13.57334,
    "last_local": 13.3324,
    "last": 13.3324,
    "buy": 13.33185,
    "sell": 13.3749
   },
  "date": ISODate("2012-12-19T10: 11: 01.439Z")
 }  

Data is inserted every minute from the price quotes and transactions.

I want to find the average for every hour from the above data.

I tried:

command(array(
 'aggregate' => 'tickers',
 'pipeline' => array( 
        array( 
       '$group' => array( 
        '_id' => array(
            'year'=>'$year($date)',
        'month'=>'$month($date)',
        'day'=>'$dayOfMonth($date)',
        'hour'=>'$hour($date)'
    ),
            'avg' => array( '$avg' => '$ticker.avg') ,
                ),                      
              )))

But only get one record as out put:

   Array ( 
     [result] => Array (
       [0] => Array ( 
             [_id] => Array ( ) 
             [avg] => 13.407843889084 ) ) 
    [ok] => 1 
  ) 

Expecting at least 100 records for 100 days...

Answers


The date operators must be used in a $project operation, not a $group, so you need to do it like this instead (in the shell):

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

Gives a result of:

{
  "result": [
    {
      "_id": {
        "year": 2012,
        "month": 12,
        "day": 19,
        "hour": 10
      },
      "avg": 13.244705635
    }
  ],
  "ok": 1
}

Need Your Help

How can I detect a rectangle with a certain dimensions in this picture, and compare the rectangle dimensions with predetermined dimensions in Matlab?

matlab image-processing

This is a real image of a battery. I want to detect the smallest rectangular which contains the full battery. Why do I want this? Because I want to know the size of this rectangular so I can compar...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.