couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Moritz Post <moritzp...@gmail.com>
Subject Calculating the sum of attachment sizes
Date Thu, 10 Jun 2010 12:31:10 GMT
Hi CouchDB

I want to calculate the sum of all attachments of a particular subset of
documents. Here is my approach:

== Map ==

function(doc) {
  var localSum = 0;
  for ( var i in doc._attachments) {
    localSum += doc._attachments[i].length
  }
  emit(doc._id, localSum);
}

== Reduce ==

function(key, values) {
  return sum(values)
}

This works fine when i run this code from within futon without providing any
subset of docs. It returns a single result { null,
<totalSumOfAllAttachments>}

But what i do in practice is using a POST on the view to provide the ids of
documents of which totalsum i am interested in (eg 2 docs here):

curl -d
'{"keys":["a813cded7cfa807c6c48ae1d84004d45","a813cded7cfa807c6c48ae1d84001823"]}'
-X POST
http://localhost:5984/mydb/_design/mydesign/_view/getAttachmentSize?group=true

Here is the problem: I have to use group=true when calling the view with
POST parameters because it is a multi-key fetch (otherwise the couch
complains). In this results i get a separate result entry for each document
since the grouping keys are obviously different for each doc._id. So i get
something like:

"a813cded7cfa807c6c48ae1d84004d45", 68346
"a813cded7cfa807c6c48ae1d84001823", 23755

but not the aggregated sum.

I have to use the doc._id in the emit function though because otherwise the
filtering via the POST parameter would not limit the sum to the document
subset i specified.

So i hope one understands the problem and is able to point me in the right
direction.

If their is any better way to calculate the sum of all attachments sizes for
a subset of documents i would also be very interested.

Thanks in advance
Moritz Post

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message