FishingIsLife
FishingIsLife

Reputation: 2372

Mongodb shell drop collections with wild card

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

Upvotes: 0

Views: 928

Answers (2)

FishingIsLife
FishingIsLife

Reputation: 2372

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.

Upvotes: 1

Gibbs
Gibbs

Reputation: 22956

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.

Upvotes: 2

Related Questions