I have a CSV file containing information on which numbers called each other and the details of the calls like duration, time, etc.
My goal is to compile all the numbers that a specific number has called into an array. Each element in this array should be a document containing all the call details as well. Essentially, I need a list of documents with a "caller" number and a nested "called" array.
To achieve this, I initially thought of using a map reduce solution, which seemed quite straightforward. However, I am facing an issue where the resulting array includes duplicate numbers instead of unique ones during the reduce phase.
The current map reduce script I'm using is as follows (executed in the Mongo shell):
db.contacts.mapReduce(
function(){
numbers = [];
value={phone:this.<<called_number>>};
numbers.push(value);
emit(this.<<caller_number>>,{called:numbers});
},
function(key,values) {
result={called:[]};
values.forEach(function (v) {
var i,j;
for(i=0;i<v.called.length;i++) {
var flag=0;
for(j=0;j<result.called.length;j++) {
if(v.called[i].phone==result.called[j].phone){
flag=1;
}
}
if(flag==0) {
result.called.push(v.called[i])
}
}
});
return result;
},
{"query": {},"out":"new_collection"}
)
I understand that both the map and reduce functions are written in JavaScript. Any help from experienced JavaScript coders on optimizing the reduce function to only consider unique numbers would be greatly appreciated.