I am reading Mongodb’s docs about Aggregation Framework and Mapreduce, but still have no clue where to begin with aggregating “columns” of integers in array. F.i. having these documents:
[{ "_id" : "A", "agent" : "006", "score" : [ 1, 0, 0 ], "qv" : [ 1, 0, 1, 0, 1 ] },
{ "_id" : "B", "agent" : "006", "score" : [ 0, 1, 0 ], "qv" : [ 1, 0, 1, 0, 1 ] },
{ "_id" : "C", "agent" : "006", "score" : [ 1, 0, 0 ], "qv" : [ 1, 0, 1, 0, 0 ] },
{ "_id" : "D", "agent" : "007", "score" : [ 1, 0, 0 ], "qv" : [ 1, 0, 1, 0, 0 ] }]
The expected result should be like:
[
{"agent": "006", "score": [2, 1, 0], "qv": [3, 0, 3, 0, 2]},
{"agent": "007", "score": [1, 0, 0], "qv": [1, 0, 1, 0, 0]}
]
Is Aggregation Framework enough for this task or should I aim for Mapreduce?
I think you’ll need map reduce for this, in order to write a function that can access specific positions in the array. You could try something like this:
Mapping function:
Reduce function:
You can then run the following mapReduce function on your collection:
And that should give you the following desired result !
}