incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Luke Randall <luke.rand...@gmail.com>
Subject Grouping by second value in key
Date Thu, 30 Apr 2009 19:48:12 GMT
Hi all

I've got a an application tracking inventory movement. Each document
contains a reference to the object, the month it occurred, and the
number of units moved.

What I'm now wanting to do is write a view to show the movement for a
specified time frame, where the code looks like this:

map: function(doc) {
  if (doc['couchrest-type'] == 'Transaction') {
      emit([doc['month'], doc['category']], (doc['amount']));
  }
}
reduce: function(keys, values) {
   return sum(values);
}

This returns items with keys like ['200904', 'Foo'], so I can specify
date ranges (and optionally the object) for the view. However, I want
the totals to be per object, not per object per month as they
currently are.

The obvious solution would be to use group_level=1, but I'm wanting to
group by the second element in the key. I've also tried switching the
order of the key's values, but I then lose the ability to specify the
date range for the view, rendering it useless.

Is there a way to either group by the second value of the key, or when
passing start and end key values, to accept all given values in the
first value of the key, but exclude them based on the second value.
I've tried something like

db.view('inventory/movement', :group => true, :startkey => [nil,
'200904'], :endkey => [{}, '200905'])

but it seems that if your have a catch all as your first value that it
ignores the second value. I've looked around for any other discussion
on this, but haven't found anything beyond what I've just mentioned.

I'd be grateful if anyone can help with this, even if it's just to
confirm that the only way to do it is to combine values at the
application level.

Thanks
Luke

Mime
View raw message