Mongodb如何批量删除指定条件的数据库?
ringa_lee
ringa_lee 2017-05-02 09:25:50
0
1
779

比方说我有1000个database,想要删除其中所有以a开头的数据库,请问怎么操作?
(注意是database不是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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!