3

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"
}
valter
  • 418
  • 9
  • 24

2 Answers2

2
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.

ermouth
  • 835
  • 7
  • 12
0

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].

Simon
  • 31,675
  • 9
  • 80
  • 92
  • Hello. Thanks for the try, but this way it won't work in my case. I will always pass for example this value startkey=[uid,authorid] - So this line of code emit(doc.uid, null); is not expecting to get an array of itens. – valter Feb 06 '15 at 12:41
  • It it was possible to just ignore the $loginid value if accepted.Y it would work. – valter Feb 06 '15 at 12:42
  • It thing it should work replacing third line by this one: emit([doc.uid],null); than it will return results whenever key has one or two values, but if I put key=[uid,logginid] it won't return the documents with accepted=Y – valter Feb 06 '15 at 12:45
  • I was suggesting you do *two distinct queries*. I'm sure there is a better way but that's a start. – Simon Feb 06 '15 at 16:09