Friday, March 23, 2012

Collection naming problems with MongoDB - Do not include hyphens

Problem: Wrongly named collections in MongoDB will cause exceptions

Why the problem occurs:
I found most of my tips from this article.
http://blog.shlomoid.com/2011/08/how-to-fix-erroneously-named-mongodb.html

Collection names should begin with letters or an underscore and may include numbers; $ is reserved. Collections can be organized in namespaces; these are named groups of collections defined using a dot notation.

You can create collections with names that include hyphens. As explained in the article above, the db.runCommand for creating a collection can look like:

db.runCommand({"create":"test-col"});
However, since the javascript console will interpret collection names with hyphens in them as subtracting letters, you will get errors.

You need to be in to your admin db (and you might need to login first, might not for local)
> use db1
> db.auth(user, pass)
> use admin
> db.runCommand ({renameCollection: "db1.test-col", to: "db1.testCol"})

If that does not work, you might need to repair your database using mongod.
http://www.mongodb.org/display/DOCS/Durability+and+Repair

For my Clojure web app, I tried to keep to a naming convention for my data (using hyphens for spaces) and my functions (using underscore for spaces) but I got a gotcha with Javascript (I think this will happen when using ClojureScript too). 

1 comment: