Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bulk operation for delete is used for delete duplicate scripts #150

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,76 +64,54 @@ function generateUUId() {

let duplicateProjectsChunk = _.chunk(duplicateArray,100);
let successfullyDeletedRecords = [];
let failedToDeletedRecords = [];

for(let i=0;i<duplicateProjectsChunk.length;i++)
{
let currentChunk = duplicateProjectsChunk[i];
for (let duplicateRecord of currentChunk) {
try {

let duplicateArray = duplicateRecord.duplicateArray;
let completedProjects = [];
let inCompleteProjects = [];
let toBeDeletedRecords = [];

for (let i = 0; i < duplicateArray.length; i++) {
if (duplicateArray[i].status == "published") {
completedProjects.push(duplicateArray[i]);
} else if (duplicateArray[i].status == "started") {
inCompleteProjects.push(duplicateArray[i]);
}
}

completedProjects.sort(sortByCreatedAtDescending);
inCompleteProjects.sort(sortByCreatedAtDescending);

if (completedProjects.length > 0) {
let firstRecord = completedProjects[0];

for (let i = 1; i < completedProjects.length; i++) {
toBeDeletedRecords.push(completedProjects[i]);
}

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;
Expand All @@ -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) {
Expand Down
36 changes: 10 additions & 26 deletions migrations/deleteDuplicateRecordsSurvey/deleteDuplicateRecords.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down