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._ import com.mongodb.casbah.Imports._ scala> val client = MongoClient() client: com.mongodb.casbah.MongoClient = com.mongodb.casbah.MongoClient@4ac17318
This connects to a MongoDB server on the default host (localhost
) and default port...