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

半世苍凉 提交于 2019-12-20 02:40:26

问题


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"
}

回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/28345751/how-to-create-a-view-in-couchdb-with-multiple-where-and-or-clauses

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!