With the following schema (defined below). I can use map reduce to aggregate the delivered_count field for all days (which is an embedded array inside the campaign document).
{
campaign_id: 1,
status: 'running',
dates: {
'20130926' => {
delivered: 1,
failed: 1,
queued: 1,
clicked: 1,
males_count: 1,
females_count: 1,
pacific_region: { clicked_count: 10 },
america_region: { clicked_count: 10 },
atlantic_region: { clicked_count: 10 },
europe_region: { clicked_count: 10 },
africa_region: { clicked_count: 10 },
etc_region: { clicked_count: 10 },
asia_region: { clicked_count: 10 },
australia_region: { clicked_count: 10 }
},
'20130927' => {
delivered: 1,
failed: 1,
queued: 1,
clicked: 1,
males_count: 1,
females_count: 1,
pacific_region: { clicked_count: 10 },
america_region: { clicked_count: 10 },
atlantic_region: { clicked_count: 10 },
europe_region: { clicked_count: 10 },
africa_region: { clicked_count: 10 },
etc_region: { clicked_count: 10 },
asia_region: { clicked_count: 10 },
australia_region: { clicked_count: 10 }
},
'20130928' => {
delivered: 1,
failed: 1,
queued: 1,
clicked: 1,
males_count: 1,
females_count: 1,
pacific_region: { clicked_count: 10 },
america_region: { clicked_count: 10 },
atlantic_region: { clicked_count: 10 },
europe_region: { clicked_count: 10 },
africa_region: { clicked_count: 10 },
etc_region: { clicked_count: 10 },
asia_region: { clicked_count: 10 },
australia_region: { clicked_count: 10 }
}
}
}
The code below parses through field asia_regions
outputs the value of field clicked_count
=> 30 (combined value of all data)
$rethinkdb.table(:daily_stat_campaigns).filter { |daily_stat_campaign| daily_stat_campaign[:campaign_id].eq 1 }[0][:dates].do { |doc|
doc.keys.map { |key|
doc.get_field(key)[:asia_region][:clicked_count].default(0)
}.reduce { |left, right|
left+right
}
}.run
Is it possible to run the code above but against multiple regions? This way I can run one query which will return multiple sums. The output i'm trying to achieve is something similar to the pseudo result below.
[{ asia_region: {clicked_count: 30}}, {america_region: {clicked_count: 30} }]