In mongo, how do I use map reduce to get a group b

2019-06-25 12:53发布

the map reduce examples I see use aggregation functions like count, but what is the best way to get say the top 3 items in each category using map reduce.

I'm assuming I can also use the group function but was curious since they state sharded environments cannot use group(). However, I'm actually interested in seeing a group() example as well.

1条回答
手持菜刀,她持情操
2楼-- · 2019-06-25 13:52

For the sake of simplification, I'll assume you have documents of the form:

{category: <int>, score: <int>}

I've created 1000 documents covering 100 categories with:

for (var i=0; i<1000; i++) {
  db.foo.save({
    category: parseInt(Math.random() * 100),
    score: parseInt(Math.random() * 100)
  });
}

Our mapper is pretty simple, just emit the category as key, and an object containing an array of scores as the value:

mapper = function () {
  emit(this.category, {top:[this.score]});
}

MongoDB's reducer cannot return an array, and the reducer's output must be of the same type as the values we emit, so we must wrap it in an object. We need an array of scores, as this will let our reducer compute the top 3 scores:

reducer = function (key, values) {
  var scores = [];
  values.forEach(
    function (obj) {
      obj.top.forEach(
        function (score) {
          scores[scores.length] = score;
      });
  });
  scores.sort();
  scores.reverse();
  return {top:scores.slice(0, 3)};
}

Finally, invoke the map-reduce:

db.foo.mapReduce(mapper, reducer, "top_foos");

Now we have a collection containing one document per category, and the top 3 scores across all documents from foo in that category:

{ "_id" : 0, "value" : { "top" : [ 93, 89, 86 ] } }
{ "_id" : 1, "value" : { "top" : [ 82, 65, 6 ] } }

(Your exact values may vary if you used the same Math.random() data generator as I have above)

You can now use this to query foo for the actual documents having those top scores:

function find_top_scores(categories) {
  var query = [];
  db.top_foos.find({_id:{$in:categories}}).forEach(
    function (topscores) {
      query[query.length] = {
        category:topscores._id,
        score:{$in:topscores.value.top}
      };
  });
  return db.foo.find({$or:query});

}

This code won't handle ties, or rather, if ties exist, more than 3 documents might be returned in the final cursor produced by find_top_scores.

The solution using group would be somewhat similar, though the reducer will only have to consider two documents at a time, rather than an array of scores for the key.

查看更多
登录 后发表回答