0

I have a bunch of collections with the same prefix e.g. data_user_1, data_user_2 etc. There are also a few collection in between that I wanted to keep and skip from dropping.

So I search for a solution to

a) drop all exclude specific b) drop all data_user_*

I searched the docs but there seems to be no parameter that allows any of these operations. Thank you

FishingIsLife
  • 1,972
  • 3
  • 28
  • 51

2 Answers2

2

Unfortunately there is no such option.

  1. You can retrieve all the available collections using getCollectionNames

  2. Then you can check for include/exclude scenarios.

  3. When the condition matches, you can perform drop on it.

Gibbs
  • 21,904
  • 13
  • 74
  • 138
1

I give up searching as there seems to be no "comfortable" way. So I have written a short script which fullfills the requirement (and is also pretty comfortable^^).

db.getCollectionNames().forEach(function(collname){
if(collname.includes('data_user')){ 
 print('delete collection: ' + collname); 
 db.getCollection(collname).drop()}
});

Hope this helps someone else too.

FishingIsLife
  • 1,972
  • 3
  • 28
  • 51