For the past few hours, I've been struggling with a seemingly simple issue. I have a collection of documents that all have the same structure:
{
"_id": "736722976",
"value": {
"tag_cloud": {
"0": {
"0": "FIFA World Cup 2014",
"1": " Germany",
"2": " Algeria",
"3": " Thomas Muller",
"4": " Mesut Ozil"
},
"1": {
"0": "Monsoon",
"1": " Germany"
}
}
}
}
I'm attempting to run a map-reduce operation on this data to count the total occurrences of each tag in the cloud. Here's what my code looks like:
var map = function(){
emit(this._id, this.value.tag_cloud);
}
var reduce = function(key, values){
var mm = new Array();
values.forEach(function(v){
for (i in v){
k = v[i].trim();
if (k in mm){
mm[k] = mm[k] + 1;
}else{
mm[k] = 1;
}
}
});
return {tag: mm};
}
db.analysis_mid.mapReduce(map, reduce,
{
out: "analysis_result"
}
);
I execute the script using the following command:
mongo localhost:27017/my_db_name_with_mother_collection mr.js
The code runs without any errors, but the output it generates is not as expected:
{
"_id": "736722976",
"value": {
"0": {
"0": "FIFA World Cup 2014",
"1": " Germany",
"2": " Algeria",
"3": " Thomas Muller",
"4": " Mesut Ozil"
},
"1": {
"0": "Monsoon",
"1": " Germany"
}
}
}
I am puzzled by this outcome and unsure of what I might be overlooking. Can anyone offer some assistance?
The desired result should look like this:
{
"_id": "736722976",
"tag": {
"FIFA World Cup 2014": 1,
"Germany": 2,
"Algeria": 1,
"Thomas Muller": 1,
"Mesut Ozil": 1,
"Monsoon": 1
}
}
Thank you for your help.