從 MongoDB 中刪除帶有特殊字元的集合?


為了刪除一些帶有特殊字元(如 _ 或 -)的集合,你需要使用以下語法 -

db.getCollection("yourCollectionName").drop();

為了理解這個概念,讓我們使用文件建立一個集合。建立一個帶有文件的集合的查詢如下:

> 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")
}

使用 find() 方法顯示一個集合中的所有文件。查詢如下:

> db.getCollection('_personalInformation').find().pretty();

\這是輸出 -

{
   "_id" : ObjectId("5c9158bb4afe5c1d2279d6b2"),
   "ClientName" : "Chris",
   "ClientCountryName" : "US"
}
{
   "_id" : ObjectId("5c9158c84afe5c1d2279d6b3"),
   "ClientName" : "Mike",
   "ClientCountryName" : "UK"
}
{
   "_id" : ObjectId("5c9158d54afe5c1d2279d6b4"),
   "ClientName" : "David",
   "ClientCountryName" : "AUS"
}

以下是如何從 MongoDB 中刪除具有特殊字元的集合的查詢 -

> db.getCollection("_personalInformation").drop();

\這是輸出 -

True

結果 TRUE 表示我們已經從 MongoDB 中完全刪除了集合。

更新於:2019 年 7 月 30 日

446 次閱讀

開啟你的 事業

完成課程後獲得認證

開始
廣告
© . All rights reserved.