couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From José Esteves <>
Subject ReduceMap
Date Thu, 21 Apr 2016 21:28:11 GMT
Hi, i’m having some trouble understanding reduce value array parameter is

Having this data (the view called with reduce=false)

{"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

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



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