开发者

CouchDB - Accessing querystring parameters in views

Is开发者_如何学Python 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);
        }
    }
}


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.


The view is pre-calculated and thus cannot take computation parameters. Then the only parameter you can use to query the view are a key or a range to retrieve only a subset of it. So what you can maybe do is use a list function to do further things to the view. Hard to say without knowing more about what you want to achieve. If you want truly dynamic queries in CouchDB you can use temporary views but it can be pretty slow as all documents in the DB will be passed to it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜