1

I have number of collection in my mongodb.

The collections names are like aaa_123,aaa_345,aaa_ccc,mmm,nnn and etc...

Many collections name has prefixed with aaa_ and also other collection name also present, I need to delete only the collection names prefixed with aaa_ , but in manual I deleted like db.aaa_123.drop(), its take long time to delete collection , Is there anyway to delete all collections with prefixed aaa_ in mongodb?

Anyhelp appreciated..

2
  • 3
    db.getCollectionInfos({ "name": /^aaa/ }).forEach( i => db.getCollection(i.name).drop() ) Commented Aug 11, 2017 at 7:16
  • Ya, Its working fine as am expected Commented Aug 11, 2017 at 7:24

1 Answer 1

3

First Get all collection in DB to one variable then loop through that variable and check for that name in collection. If that check condition satisfies delete that collection. Check below for code sample

var collectionNames = db.getCollectionNames();
for(var i = 0, len = collectionNames.length; i < len ; i++){
    var collectionName = collectionNames[i];
    if(collectionName.indexOf('aaa_') == 0){
        db[collectionName].drop()
    }
}

Hope this helps

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.