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

  • SEARCH
  • Home
  • 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 738199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:48:32+00:00 2026-05-14T07:48:32+00:00

How would I execute a query equivalent to select top 10 in couch db?

  • 0

How would I execute a query equivalent to “select top 10” in couch db?

For example I have a “schema” like so:

title   body   modified

and I want to select the last 10 modified documents.

As an added bonus if anyone can come up with a way to do the same only per category. So for:

title   category   body   modified

return a list of latest 10 documents in each category.

I am just wondering if such a query is possible in couchdb.

  • 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-14T07:48:33+00:00Added an answer on May 14, 2026 at 7:48 am

    here is what you need to do.

    Map function

    function(doc)
    {
      if (doc.category)
      {
        emit(['category', doc.category], doc.modified);
      }
    }
    

    then you need a list function that groups them, you might be temped to abuse a reduce and do this, but it will probably throw errors because of not reducing fast enough with large sets of data.

    function(head, req)
    {
      % this sort function assumes that modifed is a number
      % and it sorts in descending order
      function sortCategory(a,b) { b.value - a.value; }
      var categories = {};
      var category;
      var id;
      var row;
      while (row = getRow())
      {
        if (!categories[row.key[0]])
        {
          categories[row.key[0]] = [];
        }
        categories[row.key[0]].push(row);
      }
      for (var cat in categories)
      {
        categories[cat].sort(sortCategory);
        categories[cat] = categories[cat].slice(0,10);
      }
      send(toJSON(categories));  
    }
    

    you can get all categories top 10 now with

    http://localhost:5984/database/_design/doc/_list/top_ten/by_categories
    

    and get the docs with

    http://localhost:5984/database/_design/doc/_list/top_ten/by_categories?include_docs=true
    

    now you can query this with a multiple range POST and limit which categories

    curl -X POST http://localhost:5984/database/_design/doc/_list/top_ten/by_categories -d '{"keys":[["category1"],["category2",["category3"]]}'
    

    you could also not hard code the 10 and pass the number in through the req variable.

    Here is some more View/List trickery.

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

Sidebar

Related Questions

I have a .bat and inside the .bat i would like to execute a
I would like to execute an OS command from my ruby script but I
I would like to execute ls in a Perl program as part of a
I would like to execute multiple commands in a row: i.e. (just to illustrate
I would like to execute the jQuery $(document).ready() in a drupal site. While i
I would like to execute a stored procedure over each row in a set
In the past I would do something like so: $sql = 'SELECT * FROM
I have a query that is executed on a remote database: select /*+ DRIVING_SITE(rd)
I want to create a veiw and then select from it in one query,
(please excuse that I didn't use aliases). I would like my query output to

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.