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

วิธีลบข้อมูลทั้งหมดในฐานข้อมูล MongoDB

ลบฐานข้อมูลทั้งหมดด้วย dropDatabase()

ใช้ฟังก์ชัน dropDatabase() เพื่อลบข้อมูลทั้งหมดในฐานข้อมูล MongoDB

use yourDatabaseName; db.dropDatabase();

ขั้นตอนการลบฐานข้อมูล

  1. แสดงรายการฐานข้อมูลทั้งหมดด้วย show dbs
  2. สลับไปใช้ฐานข้อมูลที่ต้องการลบด้วย use databaseName
  3. รันคำสั่ง db.dropDatabase()
  4. ตรวจสอบอีกครั้งด้วย show dbs

ตัวอย่างการลบฐานข้อมูล 'flightInformation'

> use flighInformation;
switched to db flighInformation
> db.dropDatabase();
{ "dropped" : "flighInformation", "ok" : 1 }

ตรวจสอบผลลัพธ์

> show dbs;
admin 0.000GB config 0.000GB local 0.000GB sample 0.000GB sampleDemo 0.000GB test 0.003GB

ฐานข้อมูล 'flightInformation' ถูกลบออกจากรายการแล้ว