Last active
September 13, 2018 13:27
-
-
Save zampino/e6982a8091d2ce02075d to your computer and use it in GitHub Desktop.
Mongo DB hierarchical aggregation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
aggregate( | |
# ... | |
finalize: %Q(function(key, value) { | |
value.aggregated_at = new Date; | |
return value; | |
})) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
task aggregation_round: :environment do | |
Record.export | |
BaseCollection.aggregate_to(:daily) | |
Daily.aggregate_to(:weekly) | |
Weekly.aggregate_to(:monthly) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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