1

I'm using couch db to store subscription documents. While performing queries, I want to be able to query on multiple properties and also use an "IN" clause. For example, I have a subscriptionStatus property which can have multiple values (Active, Failed, In_Progress etc.) and subscriptions also have a customerID.

How can I create a query for all subscriptions where customerID = "JD212S" AND subscriptionStatus IN ["Active", "In_Progress"]

Essentially show me active and in progress subscriptions for a particular customer. I looked at combinations of views, multiple keys etc but I was not able to do this (or I've misunderstood the docs).

I've had a look at a number of Stack Overflow Q/A and CouchDB docs for this but seem to find options only for a single property at a time.

  • possible duplicate of [Performing a WHERE - IN query in CouchDB](http://stackoverflow.com/questions/12763430/performing-a-where-in-query-in-couchdb) – Chris Snow Feb 04 '15 at 12:52

1 Answers1

5

it does look list a duplicate, but in your context you would create a view return multiple keys then when executing that view, like the link here states, pass in your multi-key options

// view foo/bar
var view = function(doc) {
   doc.emit([doc.customerId, doc.subscriptionStatus]);
}

db.view('foo/bar', { keys: [['JD212S','Active'], 
['JD212S','In_Progress']], include_docs: true });
twilson63
  • 431
  • 3
  • 5
  • Ah thanks! I will try this. My confusion was I didn't realize I could specify the keys like so and was working out how to put them all in a single array. So essentially, each entry in the keys array is a complex key (JSON Array) in itself. Got it! It does mean though, for e.g. if I had many with many values, I'd be creating all combinations of complex key :) Anyways, thanks a lot for the answer, this is what I was looking for. – Arun Ramakrishnan Feb 05 '15 at 13:23