I broken my mind understanding mapreduce.
So, I ask you for help.
I have such entity:
public class ConstraintSpec
{
[BsonId]
public int Id { get; set; }
public bool OrderRequired { get; set; }
public ActionTypeConstraintSpec[] ActionTypeConstraintSpecs { get; set; }
}
And I’am trying to write my id generator.
Here is my “don’t working” MapReduce js code:
Map = "map = function () {" +
"emit(this._id)}";
Reduce = "reduce = function (key, values) {" +
"var max = this[0];" +
"var len = this.length;" +
"for (var i = 1; i < len; i++) if (this[i] > max) max = this[i];" +
"return max;" +
"}";
Then I writing:
var mapreduce = cont.MapReduce(Map, Reduce);
var x = mapreduce.GetResults();
But nothing works.
Please, help!
What are you trying to accomplish with map reduce, and for which field do you need to find the maximum? In your map function you need to emit the values that you are trying to aggregate/compare; you’re currently only emitting a key, this.id. Presumably the _id is unique for each document, which will prevent the reduce function from being called (reduce is not called if a single document was emitted for a particular key). The key needs to be the name of the field over which to aggregate the data.
For example, in the shell:
input data:
Map function:
Reduce function:
output data: