Connecting to MongoDB with Casbah

The official MongoDB driver for Scala is called Casbah. Rather than a fully-fledged driver, Casbah wraps the Java Mongo driver, providing a more functional interface. There are other MongoDB drivers for Scala, which we will discuss briefly at the end of this chapter. For now, we will stick to Casbah.

Let's start by adding Casbah to our build.sbt file:

scalaVersion := "2.11.7"

libraryDependencies += "org.mongodb" %% "casbah" % "3.0.0"

Casbah also expects slf4j bindings (a Scala logging framework) to be available, so let's also add slf4j-nop:

libraryDependencies += "org.slf4j" % "slf4j-nop" % "1.7.12"

We can now start an SBT console and import Casbah in the Scala shell:

$ sbt console
scala> import com.mongodb.casbah.Imports._ ...

Get Scala for Data Science now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.