如何从命令行删除 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
How do I drop a MongoDB database from the command line?
提问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 mongod
and 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, mongo
itself can act as an interpreter.
您不需要 heredocs 或 eval,mongo
它本身可以充当解释器。
#!/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 mydb
command 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 mydb
is still in use, hence inserting any input at that time will initialize the database again.
请注意,mydb
它仍在使用中,因此此时插入任何输入将再次初始化数据库。