Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MongoDB

วิธีลบคอลเลกชันทั้งหมดที่ชื่อตรงกับสตริงใน MongoDB

การลบคอลเลกชันหลายตัวที่ชื่อตรงกับ Pattern ใน MongoDB

ใช้ Loop วนตรวจสอบชื่อคอลเลกชันทั้งหมด แล้วลบคอลเลกชันที่ชื่อตรงกับ Pattern ที่กำหนด โดยใช้ drop() method

ขั้นตอนการทำงาน

  1. ดึงรายชื่อคอลเลกชันทั้งหมดด้วย db.getCollectionNames()
  2. วน Loop ตรวจสอบชื่อแต่ละตัว
  3. ถ้าชื่อตรงเงื่อนไข ใช้ db[collectionName].drop() ลบ

ตัวอย่าง: ลบคอลเลกชันที่ชื่อขึ้นต้นด้วย "delete" ใน Database "sample"

1. ดูคอลเลกชันทั้งหมดก่อนลบ

> show collections;

ผลลัพธ์ (มีคอลเลกชันที่ขึ้นต้นด้วย delete หลายตัว):

arraySizeErrorDemo
basicInformationDemo
copyThisCollectionToSampleDatabaseDemo
deleteAllRecordsDemo
deleteDocuments
deleteDocumentsDemo
deleteMultipleIdsDemo
deleteSomeInformation
documentWithAParticularFieldValueDemo
employee
findListOfIdsDemo
findMimimumElementInArrayDemo
findSubstring
getAllRecordsFromSourceCollectionDemo
getElementWithMaxIdDemo
insertDocumentWithDateDemo
internalArraySizeDemo
largestDocumentDemo
makingStudentInformationClone
oppositeAddToSetDemo
prettyDemo
returnOnlyUniqueValuesDemo
selectWhereInDemo
sourceCollection
studentInformation
sumOfValueDemo
sumTwoFieldsDemo
truncateDemo
updateInformation
userInformation

2. รัน Script ลบคอลเลกชันที่ชื่อขึ้นต้นด้วย "delete"

> var allCollectionName = db.getCollectionNames(); 
> for(var j = 0, colLength = allCollectionName.length; j < colLength; j++) { 
    var colName = allCollectionName[j]; 
    if(colName.indexOf('delete') == 0) { 
        db[colName].drop(); 
    } 
}

ผลลัพธ์:

True

3. ตรวจสอบคอลเลกชันที่เหลือ

> show collections;

ผลลัพธ์ (คอลเลกชันที่ขึ้นต้น delete หายไปแล้ว):

arraySizeErrorDemo
basicInformationDemo
copyThisCollectionToSampleDatabaseDemo
documentWithAParticularFieldValueDemo
employee
findListOfIdsDemo
findMimimumElementInArrayDemo
findSubstring
getAllRecordsFromSourceCollectionDemo
getElementWithMaxIdDemo
insertDocumentWithDateDemo
internalArraySizeDemo
largestDocumentDemo
makingStudentInformationClone
oppositeAddToSetDemo
prettyDemo
returnOnlyUniqueValuesDemo
selectWhereInDemo
sourceCollection
studentInformation
sumOfValueDemo
sumTwoFieldsDemo
truncateDemo
updateInformation
userInformation

คำอธิบายโค้ด

  • db.getCollectionNames() : คืน Array ของชื่อคอลเลกชันทั้งหมดใน Database ปัจจุบัน
  • colName.indexOf('delete') == 0 : ตรวจสอบว่าชื่อขึ้นต้นด้วย "delete" (index 0 = เริ่มต้น)
  • db[colName].drop() : ลบคอลเลกชัน (ใช้ bracket notation เพราะชื่ออยู่ในตัวแปร)

รูปแบบ Pattern อื่นๆ

// ลงท้ายด้วย "Demo"
if(colName.indexOf('Demo') === colName.length - 4) { ... }

// มีคำว่า "test" อยู่ตรงไหนก็ได้
if(colName.includes('test')) { ... }

// ใช้ Regular Expression
if(/^delete/.test(colName)) { ... }
if(/Demo$/.test(colName)) { ... }
if(/^temp_/.test(colName)) { ... }

⚠️ ข้อควรระวังสำคัญ

⚠️ อันตราย: ลบข้อมูลถาวร ไม่สามารถกู้คืนได้!

  • ทดสอบด้วย print(colName) ก่อนจริง
  • Backup Database ก่อนรัน
  • รันบน Environment ที่ถูกต้อง
  • พิจารณาใช้ renameCollection ย้ายไป Archive ก่อนลบ

วิธีปลอดภัยกว่า (Dry Run ก่อน)

> var allCollectionName = db.getCollectionNames(); 
> var toDelete = [];
> for(var j = 0; j < allCollectionName.length; j++) { 
    var colName = allCollectionName[j]; 
    if(colName.indexOf('delete') == 0) { 
        toDelete.push(colName); 
    } 
}
> print("Collections to delete:", toDelete);
// ตรวจสอบก่อน แล้วค่อยรันจริง
> toDelete.forEach(function(name) { db[name].drop(); });

ใช้งานด้วยความระมัดระวังเสมอ การลบคอลเลกชันเป็นการกระทำที่ไม่สามารถย้อนกลับได้