开发者

Access Key Parameter in Views

开发者 https://www.devze.com 2023-01-17 16:59 出处:网络
i was like to create a dynamic view in couchdb, and i\'d like to ask how to get access to parameter key in couch view.

i was like to create a dynamic view in couchdb, and i'd like to ask how to get access to parameter key in couch view. like follow :

function(doc) {
    if ((doc['couchrest-type'] == 'User') && ((doc['email'] != n开发者_运维技巧ull) || (doc['login'] != null ))) {
        if (doc['email'] == parameter[key]) {
            emit(doc['email'], doc);
        } else if (doc['login'] == parameter[key]) {
            emit(doc['login'], doc);
        }
    }
}

and what is the disadvantages for dynamic views in couchdb. and how to add such dynamic views in Couchrest Model.

Thanks, Shenouda Bertel


You cannot create dynamic views in CouchDB. You could use temporary views (see bottom of this page) to do what you're trying to do here, but temporary views notoriously have to run through your entire database to compute the result, so you're going to have absolutely horrible performance and every single CouchDB resource advises against it.

Views are useful for answering questions like "what data matches this value?" or "give me data sorted by this value". They are optimized for doing so because the map and reduce functions do not depend on the query parameters, so they can be cached and incrementally updated.

What you're trying to do is of the "what data matches this value?" kind, and so could be done with a static, permanent view:

function(doc) {
    if (doc.type == 'User') {
        if (doc.email) emit(doc.email, null);
        if (doc.login) emit(doc.login, null);
    }
}

This view lets you query any documents that have an email or login equal to a certain value, so you would simply run a query with key being the email/login you're looking for

0

精彩评论

暂无评论...
验证码 换一张
取 消