Hi
This function does not produce what I expect. It is not a cumulative sum of abstract values. Would you give comments on this? I am using mongo java driver version 3.2.2.
If I understand correctly, I think you mean “absolute values” instead of “abstract values”. Either way, I would suggest you to use the aggregation framework instead of using map-reduce.
As an example, using the two document examples you posted, you can get the sum of the ThermalDiscomfort field across all documents in the mongo shell with:
> db.test.aggregate([{$group:{_id:null, value:{$sum:{$abs:'$ThermalDiscomfort'}}}}])
{ "_id" : null, "value" : 2 }
Similar result can be achieved using the Java driver (version 3.2.2):
MongoIterable<Document> agg = collection.aggregate(
Arrays.asList(
group(null, sum("value", new Document("$abs", "$ThermalDiscomfort")))
)
);
for(Document doc : agg) {
System.out.println(doc.toJson());
}
which results in:
{ "_id" : null, "value" : 2.0 }
Please note that I used the $abs operator in the above example, which is available on MongoDB 3.2 and newer. Also, I didn’t take into account the date and the user fields in the example. Please modify the example as you require to achieve what you need.
You might find the following links helpful:
Best regards,
Kevin