mongodbmongo-shellmongo-collection

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


Solution

  • 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.