Answer the question
In order to leave comments, you need to log in
How to make a limit in mapreduce?
Hello, I'm using mongodb + built-in implementation of mapreduce.
The data looks like this: { obj: integer, likes: integer}, ...
I need to get 2 documents for each unique obj type with the maximum number of likes .
Let's say we have the following data:
1=>124, 1=>20, 1=>70, 1=>150,
3 => 500, 3=>499, 3=>0
1=>150, 1=>124,
3=>500, 3=>499
mapFunc = Code('function() {
emit(this.gid, { likes: this.likes, obj : this._id });
}')
reduceFunc = Code('function(key, values) {
var likesCount = -1;
var selectedId = -1;
values.forEach(function(value) {
if (value["likes"] > likesCount)
{
likesCount = value["likes"];
selectedId = value["obj"];
}
});
return {likes: likesCount, obj: selectedId};
}')
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question