我的数据库中有很多mongodb集合需要删除。它们都有相似的名称,如果只能使用通配符,则很容易删除它们。但它看起来并不像。
有没有办法一次选择一堆集合来删除它们?
答案 0 :(得分:22)
对于正则表达式,您可以使用string.match
db.getCollectionNames().forEach(function(c) {
if(!c.match("^system.indexes")) {
db.getCollection(c).drop();
}
});
答案 1 :(得分:8)
# Delete Particular Collections From MongoDB
> use database_name
> delete_collection_list = ["collection1", "collection2", "collection3", "collection4", "collection5", "collection6"]
> delete_collection_list.forEach( function (collection) {
if (db.getCollectionNames().indexOf(collection)>=0) {
db[collection].drop();
print("Deleted Collection: "+ collection);
}
})
答案 2 :(得分:7)
不,没有办法通过通配符/正则表达式删除多个集合。你必须逐一丢弃它们。我最近执行了类似的任务,我的脚本每秒能够减少20-30个集合。你有几个人?
答案 3 :(得分:6)
您可以使用以下命令删除所有集合。
> use database_name;
> db.getCollectionNames().forEach(function(c) {
if(c != 'system.indexes') {
db.getCollection(c).drop();
}
});
答案 4 :(得分:3)
您可以在mongo shell中使用db.dropDatabase()
删除数据库中的每个集合。在你做之前,确保你真的想要把一切都搞砸。