How does Mongodb delete databases with specified conditions in batches?
ringa_lee
ringa_lee 2017-05-02 09:25:50
0
1
801

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_lee
ringa_lee

ringa_lee

reply all(1)
过去多啦不再A梦

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template