incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tim Black <>
Subject Re: show/list
Date Fri, 15 Nov 2013 03:19:45 GMT

On 11/14/2013 06:50 PM, Filippo Fadda wrote:
> To avoid conflicts I can't update the 'article' document every time a
> user read it
You can update the 'article' document every time a user reads it by
attempting a PUT, and if the response says you got a conflict, GET the
document again and try the PUT again, until it succeeds.
> Now, I want a view that show the article and I want also show the
> impressions count. But since the impressions count can be just
> obtained querying a view (using _count as reduce func), you can't do
> it in a show, so you can't do it in a CouchApp, ouch!
What's wrong with using this map function:

function(doc) {
    if (doc.type == 'article') {
        emit(doc._id, null);
    if (doc.type == 'impression') {
        emit(doc.article_id, null);

GET ...articles_and_impressions_view?key=article_id&include_docs=true

That would get one article and all its impressions in one query.  If you
don't want to get all the impression docs because you're only interested
in the count, make another view for the impressions:

function(doc) {
    if (doc.type == 'impression') {
        emit(doc.article_id, null);

var number_of_impressions = GET

Then query for the doc separately, since you probably already know its
_id anyway.

Then format the data using templates client-side, and you've
accomplished your goal in a CouchApp without using a show or list.


View raw message