mongodump: Collection has '/' or null in the collection name

I was about to run a mongodump on my database which is hosted on Heroku instance:

mongodump --db [db-name] --host [host-name] --port [port-number]  --username [db-username] --password [db-password]

What happened was that once I tried to run the command, I've got the following error on the shell for many of my collections in my database:

Cannot dump [database-name].[collection-name]. Collection has '/' or null in the collection name.

Then I came acress this issue in MongoDb Server Issue 441 - Mongodump segfaults on certain collection names. The explanation seems to be very straightforward: MongoDB's mongodump command is trying to created a BSON file for each collection you are dumping, and / is one of characters that are not allowed to create file/directory names in UNIX/LINUX systems: /, >,<,|,:,&.


I connected to Heroku's MongoLab:mongo [host-url]:[port-number]/[db-name] -u [db-username] -p [db-password]

After that, I ran the following script in the shell to change the name of all collections that have the character / to -:

db.getCollectionNames().forEach(function(collectionName) {
   if (collectionName.indexOf("/") >= 0) {
      var collection = db.getCollection(collectionName);
      var find = '/';
      var re = new RegExp(find, 'g');
      var replaceChar = "--";
                             .replace(re, replaceChar));

Now you I could continue getting my dump! 😀😀😀

The Rstats tag of this blog is added to R Bloggers