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 3970366
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:04:39+00:00 2026-05-20T04:04:39+00:00

Please bear with me, I’m pretty new to the whole CouchDb stuff. The db

  • 0

Please bear with me, I’m pretty new to the whole CouchDb stuff.

The db looks like:

** item ** count ** user **
   A       20       bob
   B       30       bob
   C       10       bob
   D       15       john

I want to write a MapReduce that selects all the items belonging to bob and only return the top 2, sorted. so it should return [{item:"B",count:"30"},{item:"A",count:"20}]

I’m not sure how this can be done? Seems like I have to emit(doc.item, doc.count), but how do I know if the user owns the doc? How do I run another MapReduce to select the top elements?

  • 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-20T04:04:40+00:00Added an answer on May 20, 2026 at 4:04 am

    One solution would be to write your view to use a complex key, such as:

    function (doc) {
        emit([doc.user, doc.count], doc.item);
    }
    

    If you add descending=true to your query string, that would give you a view result like:

    {"total_rows":4,"offset":0,"rows":[
        {"id":"53f359b7cd360da296dd9aab3d0029bd","key":["john",15],"value":"D"},
        {"id":"53f359b7cd360da296dd9aab3d001a0e","key":["bob",30],"value":"B"},
        {"id":"53f359b7cd360da296dd9aab3d000fec","key":["bob",20],"value":"A"},
        {"id":"53f359b7cd360da296dd9aab3d002668","key":["bob",10],"value":"C"}
    ]}
    

    It’s sorted already by user, then count. (with the item type as the value)

    Then you can use a _list function to do the rest. The code below basically loops through the view, and returns the top 2 results for each user. If you specify user=bob in the query string, you’ll only get the results for bob.

    function (head, req) {
        // specify that we're sending JSON as our response
        provides('json', function () {
            var results = [],
                result, user, count, row;
    
            while (row = getRow()) {
                // if the user doesn't match the last iteration, reset our counter
                if (user != row.key[0]) {
                    user = row.key[0];
                    count = 0;
                }
    
                // we only need the top 2
                if (count++ >= 2) {
                    continue;
                }
    
                // start building a result object
                result = {
                    item: row.value,
                    count: row.key[1]
                };
    
                // if we provide user=?
                if (req.query.user) {
                    // check to see if it matches the current user
                    if (req.query.user === user) {
                        // if so, add it to the results
                        results.push(result);
                    }
                // by default, we'll return the top 2 for every user
                } else {
                    // add the user key to the result object
                    result.user = row.key[0];
                    // and add it to the result set
                    results.push(result);
                }
            }
    
            // send outside the loop, since it needs to be sent as valid JSON
            send(JSON.stringify(results));
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please bear with me as I am new to dojo, javascript, and web programming
Please bear in mind that I'm totally new to Rails when answering this.My question
Please bear with me, I'm new at C and I'm trying to program an
I'm new to iPad development so please bear with me. I have an iPad
I am new to iOS development so please bear with me. I have created
I'm new to Java development so please bear with me. Also, I hope I'm
I'm fairly new to the world of Silverlight so please bear with me. I
I am very new to using preg_replace and such so please bear with me.
I'm relatively new to C# so please bear with me. I am writing a
I'm a fairly new programmer so please bear with me on this. I am

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.