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


当前回答

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

创建drop_bad.js:

use bad;
db.dropDatabase();

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

mongo < drop_bad.js

其他回答

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

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

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

一行远程删除mongo数据库中的所有集合

注意必须使用——host, (-h为mongo命令的help), -d无选项,password后选择db和command。

mongo --host <mongo_host>:<mongo_port> -u <db_user> -p <db_pass> <your_db> --eval "db.dropDatabase()"

我发现这很容易记住:

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 }

你也可以使用"heredoc":

mongo localhost/db <<EOF
db.dropDatabase()
EOF

输出结果如下:

mongo localhost/db <<EOF
db.dropDatabase()
EOF
MongoDB shell version: 2.2.2
connecting to: localhost/db
{ "dropped" : "db", "ok" : 1 }    
bye

如果你想要更复杂的命令序列,我喜欢使用heredocs。

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