db.getCollectionNames().forEach(function(collectionName) { if (collectionName.startsWith('test')) { db.getCollection(collectionName).deleteMany({}); print(`Collection ${collectionName} has been cleared`); } });