mongoDB collection name包含特殊字符處理方法


為了刪除具有_或-等特殊字符的集合,您需要使用以下語法-

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()method的幫助下顯示集合中的所有文檔。查詢如下-

> 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中完全刪除了該集合。

如果建表時,mongoDB collection name 包含特殊字符,如\,/等,這樣在使用mongo shell時會報錯.

如下圖所示:


原因就是collection name包含特殊字符/,處理辦法是使用mongodb的getCollection函數,如:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM