is it possible? I have auto-generated mongoDB collections that at a certain moment I need to drop. I know their collection name patterns and they are too many, so dropping them by hand is not an option. All examples I had a look at using regex were involving queries but not with database commands. I know I could iterate over all collections and filtering by their name I could get it working but I am looking for a more handy and single command (I want to use it directly in the shell), if possible :)
Any suggestions?
Thank you!
You can do it using MongoDB console:
regExp = /test/;
db.getCollectionNames().filter(function(name){
return name.match(regExp)
}).forEach(function(name){
db.getCollection(name).drop()
});
You can use any regexp to match your collections names.
db.getCollectionNames().forEach(function(c) {
if(c.match("^system.indexes")) {
db.getCollection(c).drop();
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With