0

I some data from every hour that looks like this:

{
    "high" : 4686.87,
    "low" : 4671.11,
    "open" : 4671.12,
    "count" : 833,
    "volume" : 283.194184560001,
    "close" : 4678.51
}
...
{
    "high" : 4735,
    "low" : 4670.82,
    "open" : 4734.88,
    "count" : 1586,
    "volume" : 405.721894079999,
    "close" : 4671.12
}
...

I want to group then every 7 days (every 7 documents). Something like this:

db.getCollection('ticks').aggregate({$sort:{_id: -1}}, {$group: {
    id: // every 7 docs,
    open: { $first: '$open' },
    close: { $last: '$close' },
    high: { $max: '$high' },
    low: { $min: '$low' },
    volume: { $sum: '$volume' }
}});

What "id" should I use or how I add a new field that allows me to make it work?

NOTE: I know I can use the data to group it every week of the year, but don't want every week. I already have a weekly aggregated. I want to have the last 7 days aggregated, that could be spread on two weekOfYear

Adrian
  • 9,102
  • 4
  • 40
  • 35
  • You cannot group "every six documents". If you have a BSON Date value representing the time however, then you can group "every six hours". – Neil Lunn Sep 15 '17 at 21:59
  • Ok @NeilLunn, let's say I have the timestamp, how could I do that? – Adrian Sep 15 '17 at 22:01
  • @NeilLunn the question is not a duplicate. I saw that question before. I reformulated my question to make it clearer – Adrian Sep 15 '17 at 22:15
  • It's still the same answer. **ANY Interval** is computed using a modulo. There are also specific date operators for most time intervals anyway, but you can also express the math. – Neil Lunn Sep 15 '17 at 22:29

0 Answers0