const db = require('../db');
let map = function() {
  var d = new Date(this.updateTimestamp + 8 * 60 * 60 * 1000);
  var doub = function(num) {
    if ( num <= 9 ) return "0" + num;
    else return num;
  }
  emit(this.unikey, (this._id + ""));
};

let reduce = function(k, vals) {
  var result = "";
  result += vals;
  return result;
};

async function task () {
  const audit =  await db.getCollection('audit')
  const arrs = await audit.mapReduce(map, reduce, { query: {}, out: { inline : 1 } } );
  let count = 0;
  arrs.forEach( x => {
    if(x.value.indexOf(',') >= 0) {
      x.value.split(',').splice(1).forEach( async y => {
          console.dir(y);
          //const d = await audit.update({_id:db.ObjectID(y)},{$set:{removed:true}})
          //console.dir(d.result);
      });
    }
  })
}
task();

