tags:

views:

8

answers:

1

Dears,

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'] != null) || (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

A: 

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

Victor Nicollet