valter
valter

Reputation: 428

How to create a view in CouchDB with multiple WHERE and OR clauses

How would I creat a view equivalent to a SQL query like this?

SELECT * FROM bucket WHERE (uid='$uid' AND accepted='Y') OR (uid='$uid' AND authorid='$logginid')

My data is stored this way:

{
"id": 9476183,
"authorid": 85490,
"content": "some text here",
"uid": 41,
"accepted": "Y",
"time": "2014-12-09 10:44:01",
"type": "testimonial"
}

Upvotes: 3

Views: 1146

Answers (2)

ermouth
ermouth

Reputation: 844

function(doc) {
    if (doc.accepted == 'Y') {
        emit(doc.uid, null);
    }
    emit([doc.uid, doc.authorid], null);
}

One request is enough. You can tap view written by @Simon (reproduced above) using POST with param keys:[[uid, authorid], uid].

See http://docs.couchdb.org/en/latest/api/ddoc/views.html#post--db-_design-ddoc-_view-view for mode details.

Upvotes: 2

Simon
Simon

Reputation: 32953

A view could look like this:

function(doc) {
    if (doc.accepted == 'Y') {
        emit(doc.uid, null);
    }
    emit([doc.uid, doc.authorid], null);
}

You would query it with key=$uid first. If there is no match, you would query it with key=[$uid,$loginid].

Upvotes: 0

Related Questions