我不小心創建了兩個同名的數據庫。當我在蒙戈外殼做show dbs
,我得到這個:如何刪除與填充數據庫具有相同名稱的空Mongo數據庫?
> show dbs
admin (empty)
local 0.078GB
example_development (empty)
example_development 0.078GB
有沒有辦法刪除空數據庫,而不與填充的一個篡改?
我不小心創建了兩個同名的數據庫。當我在蒙戈外殼做show dbs
,我得到這個:如何刪除與填充數據庫具有相同名稱的空Mongo數據庫?
> show dbs
admin (empty)
local 0.078GB
example_development (empty)
example_development 0.078GB
有沒有辦法刪除空數據庫,而不與填充的一個篡改?
數據庫名稱不一樣,只是它有一個非打印字符,mongo shell的javascript接口爲你壓制。
在mongo中,驅動程序必須確保用戶無法向服務器提交非標準數據,但是it has been demonstrated BSON在其可以存儲的內容中非常靈活。
由於您在談論整個數據庫,解決此問題的最簡單方法是關閉mongo並刪除數據庫文件。您可以通過db.serverCmdLineOpts()
命令找到這些文件的位置。
Wes Widner answer is correct因爲你很可能有一個不可打印的字符。但是,我並不是直接從文件系統中刪除文件,而是更傾向於使用程序化方法。他們這裏的關鍵是從來沒有真正被它的字面名稱調用數據庫,而是保持一個參考變量形式的名稱:
getSiblingDB
他們每個人) 。db.getSiblingDB
並出具db.dropDatabase
use bogus;
db.createCollection('blah');
db.getCollectionNames(); // [ "blah", "system.indexes" ]
use admin;
db.adminCommand({listDatabases:1}).databases.filter(function(d){
return d.name === 'bogus';
}).map(function(d){
return d.name;
}); // [ "bogus" ]
// use [0] here since we now 'bogus' was at index 0 in previous
// getSiblingDB will allow us to issue dropDatabase on the string
// representation of the database
db.getSiblingDB(db.adminCommand({listDatabases:1}).databases.filter(function(d){
return d.name === 'bogus';
}).map(function(d){
return d.name;
})[0]).dropDatabase();
,而不是由專門name
過濾,我們想通過sizeOnDisk
過濾。請務必不要在此處選擇「管理員」,因此首先使用過濾器/映射運行adminCommand
以獲取適用的索引。機會是「管理員」將在索引0,所以你的數據庫很可能會在索引1:
db.getSiblingDB(db.adminCommand({listDatabases:1}).databases.filter(function(d){
return !d.sizeOnDisk;
}).map(function(d){
return d.name;
})[1]).dropDatabase(); // note usage of [1] here
這應該是不可能的。你能編輯你的問題來包含來自'db.adminCommand('listDatabases')'的輸出嗎? – JohnnyHK 2014-09-06 03:02:48