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 <[email protected]> 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/

Reply via email to