For my Rails+MongoId application i need to aggregate data for statistical purposes. My model is rapresenting a general Web application that has_many versions (aka: bundles) and relative users activations:
App = {
"_id" : ObjectId("4ff2e2eab528571384000eb4"),
"name" : "my app",
"category_id": "4ff2e2eab528571384000cc0",
"bundles": [{
"_id" : ObjectId("4ff2e2eab528571384000dca"),
"activations" : [{
"user" : "user_0",
"_id" : ObjectId("4ff2e2eab528571384000dd0")
},
...
},
...
]
}
Now, what i have to do is to return the number of ACTIVATIONS per application CATEGORY_ID, alĂ :
results = [
{
"_id": "4ff2e2eab528571384000cc0",
"value": 243
},
...
]
I can easily obtain the total number of BUNDLES per CATEGORY_ID with this mapreduce function:
map = function() {
for (var bundle in this.bundles) {
emit(this.category_id, 1);
}
};
reduce = function(key, values) {
var sum = 0;
values.forEach(function(value) {
sum += value;
});
return sum;
};
But, when i try to enter one level deeper, i do obtain no results at all:
map = function() {
for (var bundle in this.bundles) {
for (var activation in bundle.activations) {
emit(this.category_id, 1);
}
}
};
reduce = function(key, values) {
var sum = 0;
values.forEach(function(value) {
sum += value;
});
return sum;
};
Any idea is greatly appreciated. Best regards
Mike Costa