Help creating a view with multiple WHERE and OR


#1

Hello. I’m new to CouchBase, and I’m looking for a solution to scale my social network. Couchbase looks more interesting, specially it’s easy to scale features.

But I’m struggling about creating a view for a specific kind of document.

My documents looks like this:

{
“id”: 9476182,
“authorid”: 86498,
“content”: “some text here”,
“uid”: 41,
“accepted”: “N”,
“time”: “2014-12-09 09:58:03”,
“type”: “testimonial”
}
{
“id”: 9476183,
“authorid”: 85490,
“content”: “some text here”,
“uid”: 41,
“accepted”: “Y”,
“time”: “2014-12-09 10:44:01”,
“type”: “testimonial”
}

What I’m looking for is for a view that would be equivalent to this SQL query.

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

This way I could fetch all user’s testimonials even the ones not approved, if the user who is viewing the testimonials page is the owner of that testimonials page, or if not, show all given users testimonials where accepted is ==“Y”, plus testimonials not approved yet, but written by the user’s who is viewing the page.

If you could give me some tips about this I’ll be very grateful.


#2

Up… Sorry, but I found no solution yet. Asked in StalOverflow also, without success.


#3

First off you could try this in N1QL, though not production ready yet, the language is complete and this query works just fine in N1QL.

Second regarding views themselves try to write a view which maps to the data you’d like to see first, then filter based on startkey/endkey combos, write a reduction, or iterate through a list of UIDs or other values within your application code, to determine the Y/N value in the app. You would then retrieve a doc.id or meta.id and make a second request for the whole document via the API or another rest call in your application code.

You could do a simple view which returns the whole doc and if not return some other reference:

function (doc, meta) {
if (meta.type==“json” && (doc.accepted || doc.authorid || doc.uid)) {
if (doc.accepted==“Y”) {
emit(doc,1)
} else { emit(doc.id,doc.authorid) }
}
}

Not sure if this is exactly what you’re going but please chime in.

Just a quick note, I did this maybe a little too quickly as it will return values based on a Y or N value of accepted.
You’d need to modify the code of course but I wanted to provide an example you might further customize.