Để xóa bộ sưu tập có một số ký tự đặc biệt như _ hoặc -, bạn cần sử dụng cú pháp sau -
db.getCollection("yourCollectionName").drop(); Để hiểu khái niệm, chúng ta hãy tạo một bộ sưu tập với tài liệu. Truy vấn để tạo một bộ sưu tập với một tài liệu như sau -
> db.createCollection("_personalInformation");
{ "ok" : 1 }
> db.getCollection('_personalInformation').insertOne({"ClientName":"Chris","ClientCountryName":"US"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9158bb4afe5c1d2279d6b2")
}
> db.getCollection('_personalInformation').insertOne({"ClientName":"Mike","ClientCountryName":"UK"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9158c84afe5c1d2279d6b3")
}
> db.getCollection('_personalInformation').insertOne({"ClientName":"David","ClientCountryName":"AUS"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9158d54afe5c1d2279d6b4")
} Hiển thị tất cả các tài liệu từ một bộ sưu tập với sự trợ giúp của phương thức find (). Truy vấn như sau -
> db.getCollection('_personalInformation').find().pretty(); Sau đây là kết quả -
{
"_id" : ObjectId("5c9158bb4afe5c1d2279d6b2"),
"ClientName" : "Chris",
"ClientCountryName" : "US"
}
{
"_id" : ObjectId("5c9158c84afe5c1d2279d6b3"),
"ClientName" : "Mike",
"ClientCountryName" : "UK"
}
{
"_id" : ObjectId("5c9158d54afe5c1d2279d6b4"),
"ClientName" : "David",
"ClientCountryName" : "AUS"
} Đây là truy vấn để xóa bộ sưu tập khỏi MongoDB có một ký tự đặc biệt -
> db.getCollection("_personalInformation").drop(); Sau đây là kết quả -
True
Kết quả TRUE cho biết rằng chúng tôi đã xóa hoàn toàn bộ sưu tập khỏi MongoDB.