When running tests on a server using the recent versions of the MongoDB module for Drupal, and more specifically the MongoDB simpletests, the simpletest runner may leave droppings in your MongoDB Drupal database, which have no business remaining there. How to remove them while keeping the good collections ?
The typical case will be after a failed test runs, looking like this:
> use drupal switched to db drupal > show collections cache cache_admin_menu cache_block cache_bootstrap cache_field cache_filter cache_image cache_menu cache_page cache_path cache_token cache_views simpletest129468watchdog simpletest13031cache simpletest13031cache_bootstrap simpletest13031cache_field simpletest13031cache_form simpletest13031cache_menu simpletest311707cache simpletest311707cache_bootstrap simpletest311707cache_field simpletest311707cache_menu simpletest315780cache simpletest315780cache_bootstrap simpletest315780cache_field simpletest315780cache_menu simpletest324614watchdog simpletest885830cache simpletest885830cache_bootstrap simpletest885830cache_field simpletest885830cache_form simpletest885830cache_menu simpletest983006watchdog system.indexes system.profile watchdog
Of course, in most cases, you will just do a
db.dropDatabase() and be done with it, but sometimes you want to keep your cache, path, sessions, or watchdog eollections. Here is a simple script to do it. It's normally a one-liner, but I indented it in the gist for readability
The little trick is the use of
db[e] : in such a loop, using the usual Mongoshell-style
db.e.drop() won't work, because at this point
e is a variable containing the name of the collection, not an actual collection object, so it does not carry methods like
drop() (hence failing the former syntax), and is not itself the name of the collection (hence failing the latter syntax).