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

ลบฐานข้อมูล MongoDB จาก Command Line

ใช้คำสั่ง mongo พร้อมตัวเลือก --eval เพื่อรันคำสั่ง db.dropDatabase() จากบรรทัดคำสั่ง (Command Line) โดยไม่ต้องเข้าสู่ Mongo Shell

ไวยากรณ์

mongo yourDatabaseName --eval "db.dropDatabase()"

ตัวอย่างการใช้งาน

ตรวจสอบฐานข้อมูลที่มีอยู่:

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

ลบฐานข้อมูล StudentTracker จาก Command Prompt (Windows):

C:\Program Files\MongoDB\Server\4.0\bin>mongo StudentTracker --eval "db.dropDatabase()"
MongoDB shell version v4.0.5
connecting to: mongodb://127.0.0.1:27017/StudentTracker?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("afc34e93-b4c0-46f0-85bd-b80ed17b8c11") }
MongoDB server version: 4.0.5
{ "dropped" : "StudentTracker", "ok" : 1 }

ตรวจสอบอีกครั้ง (StudentTracker หายไป):

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

หมายเหตุ: คำสั่งนี้ต้องรันจากที่ติดตั้ง MongoDB (โฟลเดอร์ bin) และต้องมีสิทธิ์เข้าถึงฐานข้อมูล