The user can post several comments in the stream, and I'm trying to get a list of threads (individual) that the user makes a comment for him, for example: -
// 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;
It is quite simple in MySQL.
But to convert to couchdb: -
// map function(doc) { emit(doc.user_id, doc.thread_id); } // reduce function (key, thread_id) { return thread_id; }
If I use the map function above, I will get an error, for example: -
"error": "reduce_overflow_error",
"reason": "Reduce output must shrink more rapidly: Current output: ...
I think I applied the reduction function incorrectly.
If another method is used, for example: -
// map function (doc) { emit([doc.user_id, doc.thread_id], 1); } // reduce function(keys, values) { return sum(values); }
The result of group=true
looks like mysql group-by does.
However, I cannot get an ALL list of threads by the user (given that I only have user_id at the time of the request)
Thirdly, I can refuse to use a map reduction and apply directly: -
emit(doc.user_id, doc.thread_id);
And make a PHP array like
foreach ( ... ) { $threads[$thread_id] = TRUE; } array_keys($threads);
However, it is rather bloated and less effective.
The second method looks more accurate: -
key=[user_id, *] <-- it does not work, believe only work on exact match key=[user_id, thread_id] <-- return one row
Is there a way to get the result without knowing thread_id?
(ps: I'm new to couchdb and I could describe the script in a bad way)
Some link I got through @jasonsmith: - http://guide.couchdb.org/draft/cookbook.html
As a rule, the reduction function should be reduced to a single scalar value. That is an integer; line; or a small list or object of a fixed size that includes an aggregated value (or values) from an argument of values. It should never just return values ββor similar. CouchDB will give you a warning if you try to use the "wrong path":