mongodbで正規表現でcollectionを指定してremove
mongodbで正規表現を指定して特定のcollectionsだけremoveしたいケースがあったので、それのメモ。
コンソールからmongoシェルを立ち上げて下記を宣言し、宣言した関数を実行してあげるとok.
var removeUserCollections = function() { var removeTargets = []; db.getCollectionNames().forEach(function(coll){ if(coll.match(/^user_*/)){ removeTargets.push(coll); } }); print('remove following collctions'); print(removeTargets); removeTargets.forEach(function(target) { db[target].remove(); }); print('remove done!'); };