Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6881297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:05:23+00:00 2026-05-27T05:05:23+00:00

An user can post multiple comments in a thread, and I try to get

  • 0

An user can post multiple comments in a thread, and I try to get list of threads (distinct) that an user has make comment to it, like :-

// 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;

This is pretty easy 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 using the above map function, I will hit into an error like :-

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

I think I have applied the reduce function in wrong manner.

If using another way, like :-

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

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

The group=true result is look exactly what mysql group-by does.
However, I’m unable to get ALL the list of thread by an user (given I only have the user_id during query time)

Third way, I can discard use of map reduce, and directly apply :-

emit(doc.user_id, doc.thread_id);

And do an PHP array like

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

However, this is quite bloated and less efficient.

Second method look 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 all result without knowing the thread_id ?

(ps: I new to couchdb, and I might have describe the scenario in a bad manner)

Some reference I gotten via @jasonsmith :- http://guide.couchdb.org/draft/cookbook.html

As a rule of thumb, the reduce function should reduce to a single scalar value. That is, an integer; a string; or a small, fixed-size list or object that includes an aggregated value (or values) from the values argument. It should never just return values or similar. CouchDB will give you a warning if you try to use reduce “the wrong way”:

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:05:24+00:00Added an answer on May 27, 2026 at 5:05 am

    Follow closely to what this docs saying :-
    http://wiki.apache.org/couchdb/View_Snippets#Generating_a_list_of_unique_values

    // map
    function(doc)
    {
      emit([doc.user_id, doc.thread_id], null);
    }
    
    // reduce
    function (keys, values)
    {
      return null;
    }
    

    Query :-

    ?startkey=["$uid"]&endkey=["$uid",{}]&group=true
    

    And the result now is accurate,
    so the problem is lied on the reduce function and how the query being construct.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So on my site, a user can post a comment on 2 things: a
I have a view user control that can post form. This control can be
In a typical User - Post - Comment model in Rails, every user can
A user can post 4 times at most within a day. The difficulty is
I am creating a page where people can post articles. When the user posts
I want to write a page where user's can write a blog post and
By @user.posts , I can see there is a post with :unfinished status. But
I have multiple tables post id Name 1 post-name1 2 post-name2 user id username
I have a client-side interface that allows the user to perform multiple edits against
I've one object User which can have multiple Posts. Example: Load the user with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.