简体   繁体   English

如何一次删除大量mongodb集合?

[英]How to delete lots of mongodb collections at once?

There are a lot of mongodb collections in my database that I need to delete.我的数据库中有很多 mongodb 集合需要删除。 They all have similar names, and it would be easy to delete them if only wildcard characters could be used.它们都有相似的名称,如果只能使用通配符,则很容易删除它们。 But it doesn't look like they can.但看起来他们不能。

Is there a way to select a bunch of collections at once to delete them?有没有办法一次选择一堆集合来删除它们?

For regex you can use string.match对于正则表达式,您可以使用 string.match

db.getCollectionNames().forEach(function(c) {
    if(!c.match("^system.indexes")) { 
        db.getCollection(c).drop();
    }
  });
# 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.getCollection(collection).drop();
        print("Deleted Collection: "+ collection);
    }
  })

You can delete all the collections using the following command.您可以使用以下命令删除所有集合。

> use database_name;

> db.getCollectionNames().forEach(function(c) {
    if(c != 'system.indexes') { 
        db.getCollection(c).drop();
    }
  });

No, there's not a way to drop several collections by a wildcard/regex.不,没有办法通过通配符/正则表达式删除多个集合。 You have to drop them one by one.你必须一一放下它们。 I recently performed a similar task and my script was able to drop ~20-30 collections per second.我最近执行了一个类似的任务,我的脚本每秒可以删除约 20-30 个集合。 How many do you have?你有多少?

You can drop every collection in the database with db.dropDatabase() in the mongo shell.您可以在 mongo shell 中使用db.dropDatabase()删除数据库中的每个集合。 Just make sure you really want to nuke everything before you do.在你做之前,请确保你真的想核爆一切。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM