Latest sites

Quick news

  • 2014-03-27: MongoDB Watchdog module ported to Drupal 8 at the Szeged Dev Days.
  • 2014-01-26: My post on the Symfony web profiler in Silex selected in Week of Symfony. w00t !
  • 2013-10-18: My first commit went into MongoDB today. And, guess what ? It's in JavaScript
  • 2013-09-20 to 29: Working on Drupal 8 EntityAPI at the extended code sprints during and around DrupalCon Prague
  • 2012-08-19: Working on Drupal 8 EntityAPI at Drupalcon Munich
  • 2012-06-15: Working on Drupal 8 EntityAPI at DrupalDevDays Barcelona
  • 2012-03-23: Working on the future Drupal Document Oriented Storage at DrupalCon Denver. D8 or later ? Bets are on Later

Drupal tip of the day: how to drop the Simpletest collections when using Drupal with MongoDB

The problem

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

The fix

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 e.drop() or 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).