diff --git a/migrations/deleteDuplicateRecordsObservationSubmissions/deleteDuplicateRecords.js b/migrations/deleteDuplicateRecordsObservationSubmissions/deleteDuplicateRecords.js index 24c97fe5..4afee928 100644 --- a/migrations/deleteDuplicateRecordsObservationSubmissions/deleteDuplicateRecords.js +++ b/migrations/deleteDuplicateRecordsObservationSubmissions/deleteDuplicateRecords.js @@ -180,30 +180,15 @@ function generateUUId() { } } - for(let record of toBeDeletedRecords) - { - try{ - - let result = await db - .collection("observationSubmissions") - .deleteOne({ - _id:record._id - }) - - if(result.deletedCount == 1) - { - successfullyDeletedRecords.push(record._id); - } - else{ - failedToDeletedRecords.push(record._id); - } - - }catch(e){ - - failedToDeletedRecords.push(record); - } - - } + const idsToDelete = toBeDeletedRecords.map((record) => record._id); + + const result = await db.collection("observationSubmissions").deleteMany({ + _id: { $in: idsToDelete }, + }); + + if (result.deletedCount === toBeDeletedRecords.length) { + successfullyDeletedRecords.push(...idsToDelete); + } } catch (e) { console.log(e); @@ -213,7 +198,6 @@ function generateUUId() { } fs.writeFileSync('successfully_deleted_duplicated_records' + generateUUId()+'.js',JSON.stringify(successfullyDeletedRecords)) - fs.writeFileSync('failed_to_deleted_duplicated_records' + generateUUId()+'.js',JSON.stringify(failedToDeletedRecords)) console.log('Script execution completed'); connection.close(); } catch (error) { diff --git a/migrations/deleteDuplicateRecordsObservations/deleteDuplicateRecords.js b/migrations/deleteDuplicateRecordsObservations/deleteDuplicateRecords.js index 2f1a0854..6c548684 100644 --- a/migrations/deleteDuplicateRecordsObservations/deleteDuplicateRecords.js +++ b/migrations/deleteDuplicateRecordsObservations/deleteDuplicateRecords.js @@ -64,19 +64,17 @@ function generateUUId() { let duplicateProjectsChunk = _.chunk(duplicateArray,100); let successfullyDeletedRecords = []; - let failedToDeletedRecords = []; for(let i=0;i 0) { let firstRecord = completedProjects[0]; - + for (let i = 1; i < completedProjects.length; i++) { toBeDeletedRecords.push(completedProjects[i]); } @@ -98,42 +96,22 @@ function generateUUId() { for (let i = 0; i < inCompleteProjects.length; i++) { toBeDeletedRecords.push(inCompleteProjects[i]); } - - } - else if (inCompleteProjects.length > 0) { + } else if (inCompleteProjects.length > 0) { let firstRecord = inCompleteProjects[0]; for (let i = 1; i < inCompleteProjects.length; i++) { toBeDeletedRecords.push(inCompleteProjects[i]); } } - - - for(let record of toBeDeletedRecords) - { - try{ - - let result = await db - .collection("observations") - .deleteOne({ - _id:record._id - }) - - if(result.deletedCount == 1) - { - successfullyDeletedRecords.push(record._id); - } - else{ - failedToDeletedRecords.push(record._id); - } - - }catch(e){ - - failedToDeletedRecords.push(record); - } - - } - + const idsToDelete = toBeDeletedRecords.map((record) => record._id); + + const result = await db.collection("observations").deleteMany({ + _id: { $in: idsToDelete }, + }); + + if (result.deletedCount === toBeDeletedRecords.length) { + successfullyDeletedRecords.push(...idsToDelete); + } } catch (e) { console.log(e); continue; @@ -142,7 +120,6 @@ function generateUUId() { } fs.writeFileSync('successfully_deleted_duplicated_records' + generateUUId()+'.js',JSON.stringify(successfullyDeletedRecords)) - fs.writeFileSync('failed_to_deleted_duplicated_records' + generateUUId()+'.js',JSON.stringify(failedToDeletedRecords)) console.log('Script execution completed'); connection.close(); } catch (error) { diff --git a/migrations/deleteDuplicateRecordsSurvey/deleteDuplicateRecords.js b/migrations/deleteDuplicateRecordsSurvey/deleteDuplicateRecords.js index b015e3f0..b4f0500d 100644 --- a/migrations/deleteDuplicateRecordsSurvey/deleteDuplicateRecords.js +++ b/migrations/deleteDuplicateRecordsSurvey/deleteDuplicateRecords.js @@ -82,31 +82,16 @@ function generateUUId() { } } - - for(let record of toBeDeletedRecords) - { - try{ - - let result = await db - .collection("surveys") - .deleteOne({ - _id:record._id - }) - - if(result.deletedCount == 1) - { - successfullyDeletedRecords.push(record._id); - } - else{ - failedToDeletedRecords.push(record._id); - } - - }catch(e){ - - failedToDeletedRecords.push(record); - } - - } + + const idsToDelete = toBeDeletedRecords.map((record) => record._id); + + const result = await db.collection("surveys").deleteMany({ + _id: { $in: idsToDelete }, + }); + + if (result.deletedCount === toBeDeletedRecords.length) { + successfullyDeletedRecords.push(...idsToDelete); + } } catch (e) { console.log(e); @@ -116,7 +101,6 @@ function generateUUId() { } fs.writeFileSync('successfully_deleted_duplicated_records' + generateUUId()+'.js',JSON.stringify(successfullyDeletedRecords)) - fs.writeFileSync('failed_to_deleted_duplicated_records' + generateUUId()+'.js',JSON.stringify(failedToDeletedRecords)) console.log('Script execution completed'); connection.close(); } catch (error) {