Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CouchDB - Accessing querystring parameters in views

Tags:

couchdb

Is it possible access a requests querystring parameters in a view?

Consider this request... GET/database/_designs/foo/?bar=1

And this map...

views {
    foo: {
        map: function (document)
        {
            // I want to access querystring parameter "bar" here! But how?


            // I'd like to be able to do something along the lines of...

            if (bar > 0) emit(null, document);
        }
    }
}
like image 232
cllpse Avatar asked Apr 07 '11 15:04

cllpse


1 Answers

From http://sitr.us/2009/06/30/database-queries-the-couchdb-way.html:

The CouchDB design gets you great performance on large data sets. But it means that you cannot pass dynamic parameters to your map function when you run a query. You cannot ask for it to emit only user records with a given last name unless you want to maintain a special view for that particular last name. In most cases it is not practical to build separate views for every query that you might want to run someday. So what you can do is to run a query against the general purpose view above and request only key/value pairs that match a particular key.

function find_users_by_last_name(db, last_name) {
    var matches;
    matches = db.view('users/last_names', { key: last_name });
    return matches.rows.map(dot('value')); 
}

So, no, but you can query against a view.

like image 58
Chris Shain Avatar answered Sep 25 '22 02:09

Chris Shain