7

I am currently trying to create a view and query to fit this SQL query:

SELECT * FROM articles
WHERE articles.location="NY" OR articles.location="CA"
ORDER BY articles.release_date DESC

I tried to create a view with a complex key:

function(doc) { 
  if(doc.type == "Article") { 
    emit([doc.location, doc.release_date], doc) 
  }
}

And then using startkey and endkey to retrieve one location and ordering the result on the release date.

.../_view/articles?startkey=["NY", {}]&endkey=["NY"]&limit=5&descending=true

This works fine.

However, how can I send multiple startkeys and endkeys to my view in order to mimic

WHERE articles.location="NY" OR articles.location="CA" ?

1
  • While the answers are correct, it looks like this still isn't possible, it appears that this will be available whenever 2.0 comes out: issues.apache.org/jira/browse/COUCHDB-523 Commented May 14, 2015 at 15:06

2 Answers 2

7

My arch nemesis, Dominic, is right.

Furthermore, it is never possible to query by criteria A and then sort by criteria B in CouchDB. In exchange for that inconvenience, CouchDB guarantees scalable, dependable, logarithmic query times. You have a choice.

  • Store the view output in its own database, and make a new view to sort by criteria B
  • or, sort the rows afterward, which can be either
    • Sort client-side, once you receive the rows
    • Sort server-side, in a _list function. The is great, but remember it's not ultimately scalable. If you have millions of rows, the _list function will probably crash.
Sign up to request clarification or add additional context in comments.

3 Comments

I plan to have at least 50 000 docs, so sorting on the client side is out of the question:) And _list functions won't scale... so the only 2 options left are to store the view output in its own database and make a new view, or use the Lucene search engine? How would solve this? Let's say I have 50 locations, I don't want to make 50 calls to my view and then iterate through the results to get the 10 most recent articles.
@ephemere, A temporary database is not ideal, I know. However once you've got it, you can use it for any similar situation, select by A, sort by B. You'll just be fetching _view rows and turning them into _bulk_docs rows.
@ephemere, would you mind clarifying the use cases? Searching for 10 most recent articles across all locations could be solved by a different view. Also, if you are searching for a limited number of docs (say, 10 most recent articles) across arbitrary selection of locations (user request perhaps) you could always issue one search per location but limit the returned result from each one. Fetching 10 docs from each location = 30 docs.
6

The short answer is, you currently cannot use multiple startkey/endkey combinations.

You'll either have to make 2 separate queries, or you could always add on the lucene search engine to get much more robust searching capabilities.

It is possible to use multiple key parameters in a query. See the Couchbase CouchDB documentation on multi-document fetching.

4 Comments

+1. I took the liberty of linking to the docs about multiple key queries at the same time (kind of, but not quite an OR query).
The problem with this solution is that I don't want to specify a date. Let's say I want the 10 most recent articles for NY or CA, how could I write this using keys? { "keys": [["NY", {}], ["CA", {}]] }
the page on multi-document fetching is 404. You may want this: couchbase.com/docs/couchbase-single-server-1-2/…
Dead link in answer, and @Cheeso's link is also dead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.