為什麼 MongoDB 中的 SHOW DBS 命令沒有顯示我的資料庫?


這個 SHOW DBS 命令不會顯示資料庫,因為你可能還沒有為集合建立文件。如果你為集合建立了文件,那麼建立的資料庫就會可見。

讓我們實現上述概念並建立一個數據庫:

> use web;
switched to db web

以下是顯示所有資料庫的查詢:

> show dbs;

這將產生以下輸出:

admin 0.001GB
config 0.000GB
local 0.000GB
my 0.001GB
sample 0.001GB
sampleDemo 0.000GB
studentSearch 0.000GB
test 0.010GB
university 0.000GB

上面,WEB 資料庫不可見,因為我們沒有在同一個資料庫中建立集合。

為了顯示 WEB 資料庫,我們需要在同一個資料庫中建立一個包含文件的集合,如下所示:

> db.check.insertOne({"ClientName":"John"});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5cb806c2623186894665ae35")
}

以下是使用 find() 方法顯示集合中所有文件的查詢:

> db.check.find();

這將產生以下輸出:

{ "_id" : ObjectId("5cb806c2623186894665ae35"), "ClientName" : "John" }

讓我們執行上述命令來顯示所有資料庫:

> show dbs;

這將產生以下輸出:

admin 0.001GB
config 0.000GB
local 0.000GB
my 0.001GB
sample 0.001GB
sampleDemo 0.000GB
studentSearch 0.000GB
test 0.010GB
university 0.000GB
web 0.000GB

檢視上面的示例輸出,WEB 資料庫現在可見了。

更新於:2019年7月30日

3K+ 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.