删除 MongoDB 数据库中的所有内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3366397/
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
Delete everything in a MongoDB database
提问by Trevor Burnham
I'm doing development on MongoDB. For totally non-evil purposes, I sometimes want to blow away everything in a database—that is, to delete every single collection, and whatever else might be lying around, and start from scratch. Is there a single line of code that will let me do this? Bonus points for giving both a MongoDB console method and a MongoDB Ruby driver method.
我正在 MongoDB 上进行开发。出于完全非邪恶的目的,我有时想删除数据库中的所有内容——也就是说,删除每个集合,以及可能存在的任何其他内容,然后从头开始。是否有一行代码可以让我这样做?提供 MongoDB 控制台方法和 MongoDB Ruby 驱动程序方法的奖励积分。
回答by Josh K
In the mongo shell:
在 mongo 外壳中:
use [database];
db.dropDatabase();
And to remove the users:
并删除用户:
db.dropAllUsers();
回答by Rimian
Also, from the command line:
此外,从命令行:
mongo DATABASE_NAME --eval "db.dropDatabase();"
回答by Gering
I had the same problem, when I needed to reset all the collections but didn't want to loose any database users. Use the following line of code, if you would like to save the user configuration for the database:
我遇到了同样的问题,当我需要重置所有集合但不想丢失任何数据库用户时。如果您想保存数据库的用户配置,请使用以下代码行:
use <whichever database>
db.getCollectionNames().forEach(function(c) { if (c.indexOf("system.") == -1) db[c].drop(); })
This code will go through all collection names from one database and drop those which do not start with "system.".
此代码将遍历一个数据库中的所有集合名称,并删除不以“system.”开头的集合名称。
回答by DanH
I followed the db.dropDatabase()
route for a long time, however if you're trying to use this for wiping the database in between test cases you may eventually find problems with index constraints not being honored after the database drop. As a result, you'll either need to mess about with ensureIndexes, or a simpler route would be avoiding the dropDatabase alltogether and just removing from each collection in a loop such as:
我遵循这db.dropDatabase()
条路线很长一段时间,但是如果您尝试使用它在测试用例之间擦除数据库,您最终可能会发现在数据库删除后索引约束没有得到遵守的问题。因此,您要么需要使用 ensureIndexes,要么更简单的方法是完全避免 dropDatabase 并仅从循环中的每个集合中删除,例如:
db.getCollectionNames().forEach(
function(collection_name) {
db[collection_name].remove()
}
);
In my case I was running this from the command-line using:
在我的情况下,我使用以下命令从命令行运行它:
mongo [database] --eval "db.getCollectionNames().forEach(function(n){db[n].remove()});"
回答by Bogdan D
By compiling answers from @Robse and @DanH (kudos!), I've got the following solution which completely satisfies me:
通过编译@Robse 和@DanH 的答案(赞!),我得到了以下完全让我满意的解决方案:
db.getCollectionNames().forEach( function(collection_name) {
if (collection_name.indexOf("system.") == -1)
db[collection_name].drop();
else
db.collection_name.remove({});
});
Connect to you database, run the code.
连接到您的数据库,运行代码。
It cleans the database by dropping the user collections and emptying the system collections.
它通过删除用户集合和清空系统集合来清理数据库。
回答by bhv
Hear are some use full delete operations for mongodb using mongo shell
听说有一些使用 mongo shell 对 mongodb 使用完全删除操作
To delete particular document in collections: db.mycollection.remove( {name:"stack"} )
要删除集合中的特定文档: db.mycollection.remove( {name:"stack"} )
To delete all documents in 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()
回答by Mohamed El Gamal
Use
用
[databaseName]
db.Drop+databaseName();
drop collection
use databaseName
db.collectionName.drop();
回答by sjas
in case you'd need to drop everything at once: (drop all databases at once)
如果您需要一次删除所有内容:(一次删除所有数据库)
mongo --quiet --eval 'db.getMongo().getDBNames().forEach(function(i){db.getSiblingDB(i).dropDatabase()})'
回答by Selvakumar Ponnusamy
db.getCollectionNames().forEach(c=>db[c].drop())
回答by Selvakumar Ponnusamy
if you want to delete only a database and its sub-collections use this :
如果您只想删除数据库及其子集合,请使用以下命令:
use <database name>;
db.dropDatabase();
use <database name>;
db.dropDatabase();
if you want to delete all the databases in mongo then use this :
如果要删除 mongo 中的所有数据库,请使用以下命令:
db.adminCommand("listDatabases").databases.forEach(function(d)
{
if(d.name!="admin" && d.name!="local" && d.name!="config")
{
db.getSiblingDB(d.name).dropDatabase();
}
}
);