MongoDB
 sql >> Base de données >  >> NoSQL >> MongoDB

mongodb :trouver le résumé des enregistrements

Les opérateurs de date doivent être utilisés dans un $project opération, pas un $group , vous devez donc le faire comme ceci à la place (dans le 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'}
    }});

Donne un résultat de :

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