Take the 2-minute tour ×

I know you can pass a key or a range to return records in CouchDB, but I want to do something like this. Find X records that are X values.

So for example, in regular SQL, lets say I wanted to return records with ids that are 5, 7, 29, 102. I would do something like this:

SELECT * FROM sometable WHERE id = 5 OR id = 7 or id = 29 or id = 102

Is it possible to do this in CouchDB, where I toss all the values I want to find in the key array, and then CouchDB searches for all of those records that could exist in the "key parameter"?

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

You can do a POST as documented on CouchDB wiki. You pass the list of keys in the body of the request.


The downside is that a POST request is not cached by the browser.

Alternatively, you can obtain the same response using a GET with the keys parameter. For example, you can query the view _all_docs with:


which, properly URL encoded, becomes:


this should give better cacheability, but keep in mind that _all_docs changes at each doc update. Sometimes, you can workaround this by defining your own view with only the needed documents.

share|improve this answer
And performance is not the same as one query (even for a range of keys). Internally, CouchDB simply re-runs the lookup for each of the keys. All you save is round-trip HTTP latency. (Also your code might be simpler, with fewer bugs.) –  JasonSmith May 30 '11 at 0:54
add comment

With a straight view function, this will not be possible. However, you can use a _list function to accomplish the same result.

share|improve this answer
You can do it on any view (even _all_docs), but you need to do a POST instead of a GET. –  Marcello Nuccio May 29 '11 at 16:25
POSTing with a list of keys is a workaround. It's not syntactic sugar, i.e. same thing but more convenient. It is not the same thing. Internally, CouchDB looks up each key one-by-one. Performance and scalability are not the same as a GET key (or key range) lookup. However as Marcello shows in his answer, it can be very convenient indeed. –  JasonSmith May 30 '11 at 0:51
I think it depends whether the list of keys is always the same, or whether you want to be able to specify the list of keys at "run-time". If the list is a fixed list, you could create a single index (_list function) that returns the matching documents. If the list is arbitrary (a parameter), then couchdb will have to "run the lookup" for each key anyway, so you can't to better than the POST method. –  Nick Perkins Jun 1 '11 at 2:36
add comment

Your Answer


