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


当前回答

我发现这很容易记住:

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 }

其他回答

在终端执行:

mongo // To go to shell

show databases // To show all existing databases.

use <DATA_BASE> // To switch to the wanted database.

db.dropDatabase() // To remove the current database.

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

> use mydb; 
> db.dropDatabase();

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

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

您可以先切换到要删除的数据库。然后您可以使用命令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

在命令提示符中,首先使用以下命令连接mongodb:

mongo -h [host-name]:[port:number] -d [dbname] -u [username] -p [password]

您将使用<dbname>访问db。

运行如下命令删除整个数据库:

db.dropDatabase()

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

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