2011-11-30 61 views
4

一个用户可以在一个线程中发布多个注释,我试图让线程(不同)的列表中的用户做出评论它,如: -减少输出必须更快收缩,这个错误是关于什么的?

// comment table (relation table) 
id, thread_id, user_id 

select comment.thread_id, count(*) 
from user 
inner join comment on user.id=comment.user_id 
where user.id = ? 
group by comment.thread_id; 

这是在MySQL非常的方便。
但转换到CouchDB的: -

// map 
function(doc) 
{ 
    emit(doc.user_id, doc.thread_id); 
} 

// reduce 
function (key, thread_id) 
{ 
    return thread_id; 
} 

如果我使用上述地图功能,我会打成这样的错误: -

 
"error": "reduce_overflow_error", 
"reason": "Reduce output must shrink more rapidly: Current output: ... 

我想我已经在错误的应用减少功能方式。

如果使用另一种方法,如: -

// map 
function (doc) 
{ 
    emit([doc.user_id, doc.thread_id], 1); 
} 

// reduce 
function(keys, values) 
{ 
    return sum(values); 
} 

group=true结果是看正是MySQL的组通过做。
不过,我无法由用户(假设我只有在查询时间USER_ID)

三的方式来获得线程的所有名单,我可以放弃使用地图的减少,并直接适用于: -

emit(doc.user_id, doc.thread_id); 

,做一个PHP数组一样

foreach (...) 
{ 
    $threads[$thread_id] = TRUE; 
} 
array_keys($threads); 

然而,这是十分臃肿,效率较低。

第二种方法看起来更准确: -

key=[user_id, *] <-- it does not work, believe only work on exact match 

key=[user_id, thread_id] <-- return one row 

有没有办法让不知道的thread_id所有结果?

(PS:我新来的CouchDB,我可能描述的场景在一个糟糕的方式)

一些参考我通过@jasonsmith得到: - http://guide.couchdb.org/draft/cookbook.html

作为一个经验法则, reduce函数应该减少到单个标量值。也就是说,一个整数;一个字符串;或包含来自values参数的聚合值(或多个值)的小型固定大小列表或对象。它不应该只返回值或类似的东西。如果您尝试使用减少“错路”的CouchDB会给你一个警告:

回答

相关问题