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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:39:16+00:00 2026-05-24T23:39:16+00:00

I’m currently trying to order and sort by a key with three values. But

  • 0

I’m currently trying to order and sort by a key with three values. But lets start with the document structure:

{
    _id: "DOCIDGOESHERE01",
    type: "MESSAGE",
    date: "2011-08-24 06:49:02",
    author: "USERIDGOESHERE01",
    receiver: ["USERIDGOESHERE02", "USERIDGOESHERE03"],
    message: "ok let's do this"
}

The main goal is to query couchDB for messages send by selected users to one specific user and order them by the date. Some messages don’t have any receiver which indicates them to be public and can be read by anyone.

The map function i currently use looks like this:

function map(doc) {
    if(doc.receiver.lenth==0)
        emit([doc.date, null, doc.author], doc._id);
    else for(var idx in doc.receiver)
        emit([doc.date, doc.receiver[idx], doc.author], doc._id);
}

When querying the couchDB HTTP interface i tryed requests like

HTTP GET xxx/messages?key=[{}, "USERIDGOESHERE02", {}]

or

HTTP POST xxx/messages
{
    keys: [
        [{}, "USERIDGOESHERE02", "USERIDGOESHERE01"],
        [{}, "USERIDGOESHERE02", "USERIDGOESHERE03"],
        [{}, "USERIDGOESHERE02", "USERIDGOESHERE04"],
    ]
}

but all of them didn’t result in the list of documents i wanted to produce. Do you have any suggestions with this task? Or is it impossible to build such filtered results with couchDB?
Thank you very much in advance!

  • 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-24T23:39:16+00:00Added an answer on May 24, 2026 at 11:39 pm

    Keys are always sorted smallest to highest, in one long 1-dimensional list. (I have tried to describe this intuitively in The Parable of CouchDB but no idea if I succeeded!)

    What does an array sorted smallest-to-largest look like? If you read all the keys in a view, the left-hand value varies the least; the middle value varies more than the left; and the right-hand value varies the most. In other words, array keys tell CouchDB, “First priority is to sort by key[0], if that is equal, the tie-breaker will be key[1]; if those are equal also, the next tiebreaker is key[2], etc…”

    Therefore you probably want your keys to look like this:

    [ "receiver_1", null      , a_date       ],
    [ "receiver_1", "sender_A", some_date    ],
    [ "receiver_1", "sender_B", another_date ],
    [ "receiver_2", "sender_A", fourth_date  ],
    [ "receiver_3", "sender_C", fifth_date   ],
    

    To find all messages for receiver_1 from sender_B and also public messages, you need two queries, one for the "receiver_1", null pairings, and another for "receiver_1", "sender_B". You want to know any date, so you need a range of rows that match the sender/receiver. Unfortunately, the HTTP POST query does not support this.

    You could simply query for each selected sender (even all at the same time using threads or asynchronous programming). The receiver and sender are known, and this example allows a range from the smallest value (null) to the largest ({}), which will include all the dates.

    ?startkey=["receiver_1",null,null]&endkey=["receiver_1",null,{}]
    ?startkey=["receiver_1","sender_B",null]&endkey=["receiver_1","sender_B",{}]
    

    Another option is to simplify your keys and remove the dates.

    [ "receiver_1", null      ],
    [ "receiver_1", "sender_A"],
    [ "receiver_1", "sender_B"],
    [ "receiver_2", "sender_A"],
    [ "receiver_3", "sender_C"],
    [ "receiver_3", "sender_C"],
    [ "receiver_3", "sender_C"],
    [ "receiver_3", "sender_C"],
    [ "receiver_3", "sender_C"],
    

    Now you can query with the HTTP POST API again. Messages will return not ordered by date. This is not so bad, you can sort them on the client (or a _list function). And remember, even in my first example the dates are not perfectly sorted either.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
Specifically, suppose I start with the string string =hello \'i am \' me And
I am currently running into a problem where an element is coming back from
I want use html5's new tag to play a wav file (currently only supported
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.