Bubble Foundry

Connecting to a Mongo database with Casbah

by Peter.

Because I’ve wasted too much time on Casbah tonight, I have to pass it on. Simply, not much is done for you automatically. Connect like this:

val uri = MongoURI("mongodb://username:password@server:port/database")
val mongo = MongoConnection(uri)
val db = mongo(uri.database)
db.authenticate(uri.username, uri.password.foldLeft("")(_ + _.toString))
val collection = db("something")

If you call db.authenticate() a second time it will throw an exception. Lovely.