Mapreduce Couchbase自定义reduce函数

Mapreduce Couchbase自定义reduce函数,mapreduce,couchbase,Mapreduce,Couchbase,我的Couchbase中有一些文档,模板如下: { "id": 102750, "status": 5, "updatedAt": "2014-09-10T10:50:39.297Z", "points1": 1, "points2": -3, "user1": { "id": 26522, ... }, "user2": { "id": 38383, ... }, .... } 我想做的是对用户上的文档进

我的Couchbase中有一些文档,模板如下:

{
  "id": 102750,
  "status": 5,
  "updatedAt": "2014-09-10T10:50:39.297Z",
  "points1": 1,
  "points2": -3,
  "user1": {
      "id": 26522,
      ...
  },
  "user2": {
      "id": 38383,
      ...
  },
  ....
}
我想做的是对用户上的文档进行分组,并对每个用户的分数进行汇总,然后显示上周排名前100的用户。我一直在兜圈子,但还没有找到任何解决办法

我从以下映射函数开始:

function (doc, meta) {
  if (doc.user1 && doc.user2) {
    emit(doc.user1.id, doc.points1);
    emit(doc.user2.id, doc.points2);
  }
}

然后尝试求和以减少结果,但显然我错了,因为我无法对点进行排序,也无法包含日期参数

您需要查看我的示例,我可以按日期分组,并使用reduce显示值。但是计算一下我在程序中做的总数


请参阅响应

我通过服务器端脚本解决了这个问题。 我所做的是将地图功能更改为:

function (doc, meta) {
  if (doc.user1 && doc.user2) {
    emit(dateToArray(doc.createdAt), { 'userId': doc.user1.id, 'points': doc.points1});
    emit(dateToArray(doc.createdAt), { 'userId': doc.user2.id, 'points': doc.points2});
  }
}
在脚本中,我使用所需的参数查询视图,然后对它们进行分组和排序,然后发送前100名用户

我使用的是NodeJS,所以我的脚本是这样的:(结果是我从couchbase视图中读到的)


看一看-这应该与您想要实现的非常相似。Thanx@DaveRigby作为您的回复,但您提供的链接描述了如何查找指定用户的排名。我需要的是知道在特定时间段内排名前100位的用户
function filterResults(results) {
  debug('filtering ' + results.length + ' entries..');
  // get the values
  var values = _.pluck(results, 'value');
  var groupedUsers = {};

  // grouping users and sum their points in the games
  // groupedUsers will be like the follwoing:
  //  { 
  //     '443322': 33,
  //     '667788': 55,
  //     ...
  //   }
  for (var val in values) {
    var userId = values[val].userId;
    var points = values[val].points;
    if (_.has(groupedUsers, userId)) {
      groupedUsers[userId] += points;
    }
    else
      groupedUsers[userId] = points;
  }

  // changing the groupedUsers to array form so it can be sorted by points:
  // [['443322', 33], ['667788', 55], ...]
  var topUsers = _.pairs(groupedUsers);

  // sort descending
  topUsers.sort(function(a, b) {
    return b[1] - a[1];
  });

  debug('Number of users: ' + topUsers.length + '. Returning top 100 users');
  return _.first(topUsers, 100);
}