I would like to remove all collections except a list.
db.getCollectionNames().forEach(function(n){db[n].remove({})});
will remove all collections.
db.getCollectionNames().filter(function(collection){return! /^((keepthisone)|(andthisone)|(alsokeepthisone))$/.test(collection)});
will list all the collections, except the ones I want to keep.
How do I combine the two?
db.getCollectionNames().filter(function(collection){return! /^((keepthisone)|(andthisone)|(alsokeepthisone))$/.test(collection)}).forEach(function(n){db[n].remove({})});
Does nothing.
2
Answers
I would
map
thendrop
on items :I would prefer this:
You should use
getSiblingDB(...)
for security reasons. You never know if the user executed stupid command likeuse admin
before running yours. The result would be rather bad.