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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:05:19+00:00 2026-05-28T11:05:19+00:00

In couchdb I need to filter by key and which is done like this.

  • 0

In couchdb I need to filter by key and which is done like this.

{
   "_id": "_design/test",
   "_rev": "6-cef7048c4fadf0daa67005fefe",
   "language": "javascript",
   "views": {
       "all": {
           "map": "function(doc) { if (doc.blogId) {emit(doc.key, doc);} }"
       }
   }
}

However the results should be ordered by another key (doc.anotherkey). So using the same function how do I achieve both filtering and ordering by another key.

Thank you

  • 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-28T11:05:20+00:00Added an answer on May 28, 2026 at 11:05 am

    If you only need to query by single key, you can use the following map:

    function (doc) {
      if (doc.blogId) {
        emit([doc.key, doc.anotherkey], 1);
      }
    }
    

    and query for "KEY" with ?startkey=["KEY"]&endkey=["KEY",{}]&include_docs=true.

    Here, according to the collation specification of CouchDB:

    • ["KEY"] is a value lesser than any ["KEY","OTHER"] value (because longer arrays sort after their prefixes), but greater than any ["KEY2","OTHER"] with "KEY2" < "KEY";
    • and ["KEY",{}] is a value greater than any ["KEY","OTHER"] value, if doc.otherkey is never a JSON object (because JSON objects comes after any other JSON value), but lesser than any ["KEY2","OTHER"] with "KEY2" > "KEY".

    Of course this is not limited to strings. Any type of value will work, as long as the collation is right.

    Remember to URL encode the values in startkey and endkey. For example, using curl and assuming your database is “DB”:

    curl 'http://localhost:5984/DB/_design/test/_view/all?startkey=%5B%22KEY%22%5D&endkey=%5B%22KEY%22,%7B%7D%5D&include_docs=true'
    

    Note that I’ve used the include_docs query parameter, instead of emitting the entire document with emit(..., doc), to save disk space. Query parameters are documented on CouchDB documentation.

    To sort results in descending order, use the descending=true query parameter and swap the values of startkey and endkey as documented in the definitive guide book.

    curl 'http://localhost:5984/DB/_design/test/_view/all?endkey=%5B%22KEY%22%5D&startkey=%5B%22KEY%22,%7B%7D%5D&include_docs=true&descending=true'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I use couchDB with a Javascript webpage. Do I still need PHP? How
I'm trying to wrap my head around Key-Value stores like CouchDB and Cassandra. I
I'm using Mnesia with Erlang, but this question applies to any key-value db like
I'm starting to experiment with CouchDB because it looks like the perfect solution for
Can .NET (managed code) read and write to CouchDB? I would like to build
I've tested NoSQL databases like CouchDB, MongoDB and Cassandra and observed tendence to absorbing
I would like to use CouchDB to store some data for me and then
I am trying to store some cryptographic data in couchdb. I need to store
I'm using Node.js with Cradle to call a CouchDB update handler. I need to
Which PHP framework closely supports the following : API (RESTful) Nosql (mongodb / CouchDB

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.