const db = require('../db');
const moment = require('moment');
async function task(argument) {
	const recognition = await db.getCollection('dspRecognition');
	const arrs = await recognition.find({unikey:{$exists:true, "$regex": /^.{14,}$/ } }).limit(10000000000000000).toArray();
	let count = 0;
	console.dir(arrs.length);
	arrs.forEach( async item => {
		// if(unikey.length == 15){
		// 	console.dir(unikey);
		// }
		console.dir(item.unikey);
		let n = '';
		if(escape(item.unikey[0]) == 2) {
			n = item.unikey.slice(0,6) + item.unikey.slice(7,14);
			//console.dir(item.unikey.slice(7,14));
		} else {
			n = item.unikey.trim();
		}
		/*if(n.length == 13) {
			let rep = await recognition.update({_id:item._id},{ $set:{ unikey: n }  });
			console.dir(rep.result.nModified);
		}*/
		//console.dir(n.length);
	});
}

async function fixdate(argument) {
	const recognition = await db.getCollection('recognition');
	const arrs = await recognition.find({ date:{$exists:false}  }).limit(10000000000000000).toArray();
	let count = 0;
	console.dir(arrs.length);
	arrs.forEach( async item => {
		// if(unikey.length == 15){
		// 	console.dir(unikey);
		// }
		/*console.dir(item.unikey);
		let n = '';
		if(escape(item.unikey[0]) == 2) {
			n = item.unikey.slice(0,6) + item.unikey.slice(7,14);
			//console.dir(item.unikey.slice(7,14));
		} else {
			n = item.unikey.trim();
		}*/
		let date = moment(item.updateTimestamp).format('YYYYMMDD')
		let rep = await recognition.update({_id:item._id},{ $set:{ date: date }  });
		console.dir(rep.result.nModified);
		console.dir(count++);
		/*if(n.length == 13) {
			let rep = await recognition.update({_id:item._id},{ $set:{ unikey: n }  });
			console.dir(rep.result.nModified);
		}*/
		//console.dir(n.length);
	});
}

//task();
fixdate();

