Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

couchdb

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"
}
like image 629
valter Avatar asked Jan 23 '26 19:01

valter


1 Answers

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.

like image 128
ermouth Avatar answered Jan 26 '26 23:01

ermouth