Skip to content

Instantly share code, notes, and snippets.

@zampino
Last active September 13, 2018 13:27
Show Gist options
  • Save zampino/e6982a8091d2ce02075d to your computer and use it in GitHub Desktop.
Save zampino/e6982a8091d2ce02075d to your computer and use it in GitHub Desktop.
Mongo DB hierarchical aggregation
class BaseCollection
include Mongoid::Document
aggregate(:daily,
map: %Q(function() {
var key = {
year_day: this.year_day,
year_week: this.year_week,
year_month: this.year_month
};
var value = {
count: 1
};
emit(key, value);
}),
reduce: %Q(function(key, values) {
var result = {
count: 0
};
values.forEach(function(value){
result.count += value.count;
});
return result;
}))
end
class Daily
include Mongoid::Document
aggregate(:weekly,
map: %Q(function() {
var key = {
year_week: this._id.year_week,
year_month: this._id.year_month
};
var value = {
count: 1
};
emit(key, value);
}),
reduce: %Q(function(key, values) {
var result = {
count: 0
};
values.forEach(function(value){
result.count += value.count;
});
return result;
}))
end
aggregate(
# ...
finalize: %Q(function(key, value) {
value.aggregated_at = new Date;
return value;
}))
class Record < AR::Base
# my dear SQL table exports itself to a MongoDB collection
def self.export
all.each {|record|
date = reccord.created_at
document = {}
document[:year_day] = date.strftime('%Y-%m-%d') # also %j
document[:year_week] = date.strftime('%Y-%W')
document[:year_month] = date.strftime('%Y-%m')
# ... whatever makes your document a 'nice document' ...
BaseCollection.create(document)
}
end
end
task aggregation_round: :environment do
Record.export
BaseCollection.aggregate_to(:daily)
Daily.aggregate_to(:weekly)
Weekly.aggregate_to(:monthly)
end
class Weekly
include Mongoid::Document
aggregate(:monthly,
map: %Q(function() {
var key = {
year_month: this._id.year_month
};
var value = {
count: 1
};
emit(key, value);
}),
reduce: %Q(function(key, values) {
var result = {
count: 0
};
values.forEach(function(value){
result.count += value.count;
});
return result;
}))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment