Arun Ramakrishnan
Arun Ramakrishnan

Reputation: 128

Query CouchDB with multiple keys for multiple properties

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.

Upvotes: 1

Views: 1299

Answers (1)

twilson63
twilson63

Reputation: 441

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 });

Upvotes: 5

Related Questions