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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:33:07+00:00 2026-06-15T22:33:07+00:00

I’m stumbling a bit with my CouchDB knowledge. I have a database of content

  • 0

I’m stumbling a bit with my CouchDB knowledge.

I have a database of content that is tagged with an array of tags and has a created date.

I want to create a view that pulls a limited number of newest stories tagged with a specific tag.

For example, the newest 6 stories tagged “Business.”

Ran across this question, which seems to get me almost to where I need to go, but I’m missing one key element, which I think is how to craft the query string to sort by one key while searching by the other.

Here’s my map function.

function(doc) {
    if (doc.published == "yes" && doc.type == "news") {
        for (var i = 0; i < doc.tags.length; i++) {
            if (doc.tags[i]) {
                emit([doc.created, doc.tags[i]], doc);
            }
        }
    }
}

So how do I query that view for a all documents tagged “Business” that are the newest documents based on created.

The created attribute is a date sortable format.

  • 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-06-15T22:33:08+00:00Added an answer on June 15, 2026 at 10:33 pm

    First, I would switch the order of your emit:

    emit([doc.tags[i], doc.created]);
    

    (leave out doc as well, you can just add include_docs=true to get the entire document, and your view won’t take up so much disk-space in the process)

    Now you can query for the all the stories tagged as "Business" by using the following querystring:

    startkey=["Business"]&endkey=["Business",{}]
    

    You’ll get all the documents with the tag business, and they’ll be sorted by date.

    This takes advantage of view collation, which basically is the rules governing how indexes are sorted/queried. For complex keys like this, the sorting is done for each item of the array separately. (ie. the first key is sorted first, the second key is sorted second, etc) This is why the order matters, as you must always move from left to right when querying a view index.

    If you want the 6 most recent, your querystring will need to change:

    descending=true&limit=6&endkey=["Business"]&startkey=["Business",{}]
    

    NOTICE You need to swap the startkey/endkey values, due to how the descending parameter works. See the View reference page on the wiki for further explanation.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an array which has BIG numbers and small numbers in it. I
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a reasonable size flat file database of text documents mostly saved in

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.