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


当前回答

从mongo shell中使用以下命令删除db

use <database name>; 
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 }

您可以先切换到要删除的数据库。然后您可以使用命令dropDatabase()删除相同的。

代码:

>用dbName

>db.dropdataBase()

结果将是:

{ "dropped" : "dbName", "ok" : 1 }

如果要删除数据库中的特定集合,则切换到数据库并输入以下命令。

代码:

>用dbName

db.collection.drop ()

结果将是:

true

如果你想更好地理解MongoDB shell命令,它是 最好始终遵循文档。 文档链接: https://www.mongodb.com/docs/manual/reference/method/#std-label-js-administrative-methods

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

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

使文件可执行并运行它。

db将显示当前的数据库名称 类型:db.dropDatabase ();

1-使用'use'关键字选择要删除的数据库。

2-然后输入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()"