15

I'm having a problem getting a couchdb view to return the proper documents when using multiple keys.

This works fine, returning the documents that match:

GET http://example.com:5984/myDb/_design/myFilters/_view/getItemByForeignKeyId?key=abc123

This returns returns all documents in the view, matching or not:

GET http://example.com:5984/myDb/_design/myFilters/_view/getItemByForeignKeyId?keys=%5B%22abc123%22%5D

I'm usually very good at hunting down my answers. But, CouchDB documentation is very clear on the format for using multiple keys. I've seen some use the ?keys=[123,123] and i've also seen ?keys="abc","abc".

If anyone can offer any clarification on the 'proper' format and encoding of multiple key queries for CouchDB using a GET method, I would be extremely appreciative.

3 Answers 3

21

To get multiple keys from a view, you need to do a post request and submit the keys in the request body. Your HTTP request will look like this:

POST /myDb/_design/myFilters/_view/getItemByForeignKeyId
Content-Type: application/json

{
   "keys" : [
      "abc",
      "123"
   ]
}
1
  • 1
    Thanks you so much. That worked. The wording of the CouchDB documentation can be ambiguous at times.
    – J. Lincoln
    May 27, 2013 at 2:31
9
function(doc){
    {
        if([doc.key1, doc.key2])
            emit([doc.key1, doc.thingYouWantToKnow]);
    }
}

and in the query string, at the end

?key=["key1Value", "key2Value"]

Notice that it is key=[], not keys=[] !!!!!!!!!

0
5

Not saying it's correct, but you can actually do it via query string as well. The array enclosing brackets should not be encoded. E.g. this works for me:

http://localhost:5984/test/_design/artists_albums/_view/albums_by_artist?keys=[%22Super%20bad%20artist%22,%20%22Fake%20artist%201%22]

1
  • 1
    It's not documented. Is it ok to use it or should I opt for the POST request.
    – albertjan
    Jan 28, 2015 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.