search

Home  >  Q&A  >  body text

How does Mongodb delete databases with specified conditions in batches?

For example, I have 1,000 databases and want to delete all databases starting with a. How do I do this?
(note database not collection)

ringa_leeringa_lee2788 days ago806

reply all(1)I'll reply

  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-02 09:27:50

    It requires a little skill, but it’s not troublesome, just a script:

    db.runCommand({listDatabases: 1}).databases.forEach(function(database) {
        if(database.name.match(/^a/)) {
            db.getDB(database.name).dropDatabase();
        }
    });

    Probably just pass listDatabase得到所有的库,然后从中找到符合你条件的库,然后dropDatabase()删除掉。
    注意避开关键的系统库,比如local, config, admin and wait

    reply
    0
  • Cancelreply