从bash提示符中最简单的方法是什么?


当前回答

为了确保你放弃了正确的数据库使用

mongo <connection properties> --eval "db.getSiblingDB('dbname').dropDatabase()" 

在尝试保存到mongodb时,请参阅身份验证失败以了解问题。

其他回答

其他方式:

echo "db.dropDatabase()" | mongo <database name>

尽管有几种方法,但最好的(最有效和最简单的)方法是使用db.dropDatabase()

最好的方法是从mongodb控制台执行:

> use mydb; 
> db.dropDatabase();

或者,您可以停止mongod并从数据目录中删除数据文件,然后重新启动。

提示:您还可以将数据文件移动到子文件夹中,如果您确定不再需要它们,可以删除它们。

使用Javascript,你可以很容易地创建drop_bad.js脚本来删除你的数据库:

创建drop_bad.js:

use bad;
db.dropDatabase();

然后在终端上用mongo shell命令执行脚本:

mongo < drop_bad.js

我发现这很容易记住:

mongo //to start the mongodb shell

show dbs //to list existing databases

use <dbname> //the <dbname> is the database you'd like to drop

db //should show <dbname> just to be sure I'm working with the right database

db.dropDatabase() //will delete the database & return { "dropped" : "<dbname>", "ok" : 1 }