couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From José Esteves <ajeste...@gmail.com>
Subject Re: ReduceMap
Date Fri, 22 Apr 2016 13:03:44 GMT
Thanks for you're reply Jan!
For what i've checked, the descending parametre do afects the order of the
value array parametre in the reduce function.
Look the results i'm gettin when i call the same view with one or other

with descending=false

{"key":null,"value":[{"dest":"B","ids":[1,2],"cant":2},{"dest":"A","ids":[3,4,5],"cant":3}]}

descending=true

{"rows":[
{"key":null,"value":[{"dest":"A","ids":[1,2,3,4,5],"cant":5}]}
]}

with this map/reduce function

{
  "_id": "_design/movs",
  "_rev": "131-bfc19def2bea6bda0784a609b2ba3f25",
  "views": {
    "unwind": {
      "map": "function(doc)
{doc.animals_ids.forEach(function(id){emit(doc._id, {anid: id, dest:
doc.destiny}) })} ",
      "reduce": "function(key,values){var res = [];var ac=[];var rt= {};
values.forEach(function(i){if(ac.indexOf(i.anid)==-1){ac.push(i.anid);rt[i.dest]?rt[i.dest].push(i.anid):rt[i.dest]=[i.anid]
}}); for(key in rt){res.push({dest: key, ids:rt[key], cant:rt[key].length})
}; return res }"
    }
  },
  "language": "javascript"
}


it clearly affecting the order of how the array value arrives to the reduce
function. What I can't get is why the array order es the inverse of the map
function.

How would it be the couch way of achieving this query.
data:[
{_id: 1,
depot : A,
ids: [1,2,3,4,5]},
{_id: 2,
depot: B,
ids: [1,2]},
{_id: 3,
depot: C,
ids: [5]}
]

I want to get a result that shows me witch ids are in every deposit. If the
id is more than once the last _id document is the one to consider.
in this example y should expect
depot A: [3,4]
depot B: [1,2]
depot C: [5]

than for you're help



El vie., 22 de abr. de 2016 a la(s) 04:54, Jan Lehnardt <jan@apache.org>
escribió:

> Heya,
>
> the order of values is actually not defined, the decending parameter
> only affects the map part of views.
>
> Best
> Jan
> --
>
> > On 21 Apr 2016, at 23:28, José Esteves <ajesteves@gmail.com> wrote:
> >
> > Hi, i’m having some trouble understanding reduce value array parameter is
> > ordered.
> >
> > Having this data (the view called with reduce=false)
> >
> >
> > {"total_rows":7,"offset":0,"rows":[
> > {"id":"1","key":"1","value":{"anid":1,"dest":"A"}},
> > {"id":"1","key":"1","value":{"anid":2,"dest":"A"}},
> > {"id":"1","key":"1","value":{"anid":3,"dest":"A"}},
> > {"id":"1","key":"1","value":{"anid":4,"dest":"A"}},
> > {"id":"1","key":"1","value":{"anid":5,"dest":"A"}},
> > {"id":"2","key":"2","value":{"anid":1,"dest":"B"}},
> > {"id":"2","key":"2","value":{"anid":2,"dest":"B"}} ]}
> >
> > with this functions
> >
> > {
> >  "_id": "_design/movs",
> >  "_rev": "131-bfc19def2bea6bda0784a609b2ba3f25",
> >  "views": {
> >    "unwind": {
> >      "map": "function(doc)
> > {doc.animals_ids.forEach(function(id){emit(doc._id, {anid: id, dest:
> > doc.destiny}) })} ",
> >      "reduce": "function(key,values){var res = [];var ac=[];var rt=
> > {};
> values.forEach(function(i){if(ac.indexOf(i.anid)==-1){ac.push(i.anid);rt[i.dest]?rt[i.dest].push(i.anid):rt[i.dest]=[i.anid]
> > }}); for(key in rt){res.push({dest: key, ids:rt[key],
> > cant:rt[key].length}) }; return res }"
> >    }
> >  },
> >  "language": "javascript"
> > }
> >
> > I’m getting this result when calling the view without params
> >
> >
> > {"rows":[
> {"key":null,"value":[{"dest":"B","ids":[1,2],"cant":2},{"dest":"A","ids":[3,4,5],"cant":3}]}
> > ]}
> >
> > But I was expecting to get to this result after passing the parameter
> > descending=true not before. The only explanation I’ve arrived is that the
> > value array in reduce function is generated on the inverse order of the
> map
> > order.
> >
> > The reduce function objective is to pass only the first  repetition of
> each
> > id. When the same id appears on the loop it is ignore.   But newer
> > documents, with higher id are taken precedence, over olders docs
> >
> >
> > if someone could take a look to see if im doing something wrong I'll
> > appreciate
> >
> >
> > thanks,
> >
> >
> >
> >
> >
> >
> >
> > ​
>
> --
> Professional Support for Apache CouchDB:
> https://neighbourhood.ie/couchdb-support/
>
>

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