couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mark Hahn <m...@hahnca.com>
Subject Re: what's the order of docs/keys going into reduce func?
Date Sat, 22 Sep 2012 18:43:14 GMT
>  i want a view for all lamps that the currently off.

You view is wrong.  You should only emit when the lamp is off.

Also, you should always return a value in reduce.  Returning null will
screw things up.

I would start by fixing these problems and then go from there.


On Sat, Sep 22, 2012 at 3:05 AM, svilen <az@svilendobrev.com> wrote:

> say i have some lamp status monitoring records, i.e.
>
>  { "seq": 1, "name": "a", },
>  { "seq": 2, "name": "b", },
>  { "seq": 2, "name": "a", "on": true},
>  { "seq": 3, "name": "a", },
>  { "seq": 7, "name": "a", "on": true},
>
> ...
>
> where default state is off, and when on, there's extra bool attribute
> for it. Records are sequenced (in time) by some .seq param. And there's
> many other here-irrelevant attributes.
>
> say i want a view for all lamps that the currently off.
>
> so, something like:
>
> map_func: function( doc) {
>   if (doc.type == 'lamp') emit( [doc.name, doc.seq], doc );
> }
>
> reduce_func: function( keys, values, rereduce) {
>   var v = values[ values.length-1];
>   if (v == null || v.on) return null;
>   return v;
> }
>
> always used with group_level= 1
>
> but i get the very first state instead of very last state...
>
> and in the debug-log, i see that the keys passed to reduce are reversed!
>
> so i have to use descending=True, and only then it works correctly.
>
> the question is, is this expected, or is the default order defined
> somewhere else, or what?
>
> ciao
> svil
>

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