0

I have a view whereby key is the date (as I would like to query by date) and the value is an object with two pieces of information i.e.:

  {
    key: [ 2021, 4, 23 ],
    value: { 
             vimr: '7b715c70-2689-4241-a07e-450c8aebf27c', 
             prod: '392799267' 
    }
  },
  {
    key: [ 2021, 6, 17 ],
    value: { 
             vimr: '4845a8f6-39c8-419e-9b22-1c1bc7ef432b', 
             prod: '768298103' 
    }
  },
  {
    key: [ 2021, 6, 17 ],
    value: { 
             vimr: '7b715c70-2689-4241-a07e-450c8aebf27c', 
             prod: '768298103' 
    }
  },
  {
    key: [ 2021, 4, 2 ],
    value: { 
             vimr: '7b715c70-2689-4241-a07e-450c8aebf27c', 
             prod: '392799267' 
    }
  }

I'm looking to reduce this information to remove duplicates and minimise the amount of data that is returned from a query. Looking at the documentation, it seems the input for the values parameter to the reduce function is an array like so:

  [
     { 
         vimr: '7b715c70-2689-4241-a07e-450c8aebf27c', 
         prod: '392799267' }
     },
     { 
         vimr: '4845a8f6-39c8-419e-9b22-1c1bc7ef432b', 
         prod: '768298103' 
     },
     { 
         vimr: '7b715c70-2689-4241-a07e-450c8aebf27c', 
         prod: '768298103' 
     },
     { 
         vimr: '7b715c70-2689-4241-a07e-450c8aebf27c', 
         prod: '392799267' 
     }
   ]

I have tried to condense the information with the following map reduce function:

function (keys, values) {
  var result = {};

  for (var k in values){
      var data = values[k];
      
      if (!result[data.vimr]){
        result[data.vimr] = {}
      }
      
      result[data.vimr][data.prod] = true;
  }
    
    return result;
}

However I seem to just get a return value of undefined from the query. I'm struggling to understand where I'm going wrong, or even how I can debug this. Any advice would be much appreciated.

  • Is `prod` really the same for any given date? And `vimr` doesn't vary? – RamblinRose Mar 01 '21 at 17:41
  • No, I have only selected a few, there is much more data. You can have any combination of vimr and prod and they are unrelated to date. – Liam McNulty Mar 01 '21 at 18:30
  • OK great, so it's not clear to me what is meant by "remove duplicates and minimise", do you mean duplicate dates? – RamblinRose Mar 01 '21 at 19:26
  • Does this answer your question? [A CouchDB view returning one element for each per "group"](https://stackoverflow.com/questions/66173759/a-couchdb-view-returning-one-element-for-each-per-group) – RamblinRose Mar 02 '21 at 00:04
  • Also possibly a duplicate of [CouchDB display distinct values](https://stackoverflow.com/questions/66231293/couchdb-display-distinct-values/66234355#66234355) – RamblinRose Mar 02 '21 at 00:05

0 Answers0