如何从命令行删除 MongoDB 数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8857276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:25:00  来源:igfitidea点击:

How do I drop a MongoDB database from the command line?

mongodb

提问by coffee-grinder

What's the easiest way to do this from my bash prompt?

从我的 bash 提示符执行此操作的最简单方法是什么?

回答by Tim Gautier

Like this:

像这样:

mongo <dbname> --eval "db.dropDatabase()"

More info on scripting the shell from the command line here: https://docs.mongodb.com/manual/tutorial/write-scripts-for-the-mongo-shell/#scripting

有关从命令行编写 shell 脚本的更多信息:https: //docs.mongodb.com/manual/tutorial/write-scripts-for-the-mongo-shell/#scripting

回答by mnemosyn

The best way to do it is from the mongodb console:

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

> use mydb; 
> db.dropDatabase();

Alternatively, you can stop mongodand delete the data files from your data directory, then restart.

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

Hint: you can also move the data files to a subfolder, and delete them if you're sure you no longer need them.

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

回答by Alex Baban

I found this easy to remember:

我发现这很容易记住:

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 }

回答by mikemaccana

You don't need heredocs or eval, mongoitself can act as an interpreter.

您不需要 heredocs 或 evalmongo它本身可以充当解释器。

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

Make the file executable and run it.

使文件可执行并运行它。

回答by mikemaccana

Start MongoDB

启动MongoDB

Command for Database drop is :

数据库删除命令是:

1.first select the database which you want to delete

1.首先选择要删除的数据库

use < database name >

2.Then use this..

2.然后用这个..

db.dropDatabase()

回答by David

You could also use a "heredoc":

您还可以使用“heredoc”:

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

Results in output like:

结果输出如下:

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

I like to use heredocs for things like this, in case you want more complex sequence of commands.

我喜欢将 heredocs 用于这样的事情,以防您想要更复杂的命令序列。

回答by Gabriel Mancini

Other way:

另一种方式:

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

回答by bhv

Here are some use full delete operations for mongodb using mongo shell

下面是一些使用 mongo shell 对 mongodb 的完整删除操作

To delete particular documentin collections: db.mycollection.remove( {name:"stack"} )

删除集合中的特定文档db.mycollection.remove( {name:"stack"} )

To delete all documentsin collections: db.mycollection.remove()

删除集合中的所有文档db.mycollection.remove()

To delete collection: db.mycollection.drop()

删除集合db.mycollection.drop()

to delete database: first go to that database by use mydbcommand and then

删除数据库:先去该数据库由use mydb命令,然后

db.dropDatabase()

directly from command prompt or blash : mongo mydb --eval "db.dropDatabase()

直接从命令提示符或 blash : mongo mydb --eval "db.dropDatabase()

回答by Anderson Lopes

Execute in a terminal:

在终端中执行:

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.

回答by helcode

Open another terminal window and execute the following commands,

打开另一个终端窗口并执行以下命令,

mongodb
use mydb
db.dropDatabase()

Output of that operation shall look like the following

该操作的输出应如下所示

MAC:FOLDER USER$ mongodb
> show databases
local      0.78125GB
mydb       0.23012GB
test       0.23012GB
> use mydb
switched to db mydb
>db.dropDatabase()
{ "dropped" : "mydb", "ok" : 1 }
>

Please note that mydbis still in use, hence inserting any input at that time will initialize the database again.

请注意,mydb它仍在使用中,因此此时插入任何输入将再次初始化数据库。