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


当前回答

登录到mongoDB命令行: 并键入下面的命令。 使用“YOUR_DATABASE_NAME”; db.dropDatabase ();

其他回答

登录到mongoDB命令行: 并键入下面的命令。 使用“YOUR_DATABASE_NAME”; db.dropDatabase ();

一行远程删除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 }

很惊讶我们没有看到这种变化。这样可以最小化命令行上的额外参数,并显式地显示DB被切换到FOO,然后被删除:

$ mongo --host "mongodb://machine:port" --eval 'db.getSiblingDB("FOO").dropDatabase();'

你不需要heredocs或eval, mongo本身就可以充当解释器。

#!/usr/bin/env mongo
var db = new Mongo().getDB("someDatabase");
db.dropDatabase();

使文件可执行并运行它。