// tslint:disable:no-console import * as moment from 'moment'; import * as mongoose from 'mongoose'; import { chevre } from '../../../lib/index'; // const project = { id: '' }; // const MASKED_PROFILE = '****'; async function main() { await mongoose.connect(process.env.MONGOLAB_URI); const accountingReportRepo = new chevre.repository.AccountingReport(mongoose.connection); const cursor = await accountingReportRepo.accountingReportModel.find( { 'mainEntity.orderDate': { $lt: moment() .add(-1, 'year') .toDate() } }, { // _id: 1, } ) // .limit(10) .sort({ 'mainEntity.orderDate': chevre.factory.sortType.Descending }) .cursor(); console.log('reports found'); let i = 0; let updateCount = 0; await cursor.eachAsync(async (doc) => { i += 1; const accountingReport = doc.toObject(); console.log( 'deleting report...', accountingReport.id, accountingReport.mainEntity.orderNumber, accountingReport.mainEntity.orderDate, i); await accountingReportRepo.accountingReportModel.findByIdAndDelete( accountingReport.id ) .exec(); updateCount += 1; console.log( 'report deleted', accountingReport.id, accountingReport.mainEntity.orderNumber, accountingReport.mainEntity.orderDate, i); }); console.log(i, 'reports checked'); console.log(updateCount, 'reports updated'); } main() .then() .catch(console.error);