Using MongoDB in Scala with Salat

Table of contents
Reading Time: 2 minutes

In our last post, we had seen how easily we could access MongoDB with the Casbah driver. In this post, we would have a sneak peak at Salat.

Salat is a bidirectional, serialization library which helps us work with MongoDB’s DBObject. To quicly get started with Salat, you would need to include the following dependency in your Build.scala We would also need the dependency for the Casbah driver. The Build.scala would look like

[source language=”scala”]
object ApplicationBuild extends Build {

val appName = "MyCoolPlay"
val appVersion = "1.0"

val appDependencies = Seq(
"com.mongodb.casbah" %% "casbah" % "2.1.5-1",
"com.novus" %% "salat-core" % "0.0.8-SNAPSHOT"
// Add your project dependencies here,
)

val main = PlayProject(appName, appVersion, appDependencies).settings(defaultScalaSettings:_*).settings(
// Add your own project settings here
)
}
[/source]

Salat works with case classes. Let us define our model as something like this

[source language=”scala”]
case class Stream(@Key("_id") id: Int, name: String, streamType: String, creator: String)
[/source]

Now, we create our DAO which extends the SalatDAO. The SalatDAO is a helper wrapper which eradicates the need to have a lot of boiler plate code. It returns a MongoCursor that extends Iterable typed to our case class. We can limit, skip, sort and then call toList on our results.

For our case, we define the StreamDAO as

[source language=”scala”]
object StreamDAO extends SalatDAO[Stream, Int](collection = MongoConnection()("test")("stream"))
[/source]

Now, let us write a test to see that we are able to access the database using the DAO

[source language=”scala”]
import org.scalatest.FunSuite
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import com.sun.org.apache.xalan.internal.xsltc.compiler.ForEach
import org.scalatest.BeforeAndAfter
import com.mongodb.casbah.commons.MongoDBObject

@RunWith(classOf[JUnitRunner])
class SmallStreamTest extends FunSuite with BeforeAndAfter {

val stream1 = Stream(100, "al1pha", "class", "vikas")

before {
StreamDAO.insert(stream1)
}

test("Insert and retrieve a stream") {

val streams = StreamDAO.find(MongoDBObject("name" -> ".*".r))
assert(streams.size === 1)

}
after {
StreamDAO.remove(stream1)
}
}
[/source]

As you can see, we can insert our object using StreamDAO.insert(stream1) and retrieve it using one of the many convenience methods provided by StreamDAO i.e StreamDAO.find(MongoDBObject(“name” -> “.*”.r))

Here we are using a regular expression “.*” to match all the names.

In our next post, we will look at accessing this functionality via Play framework so that we can display it using a Play controller.

Written by 

Vikas is the CEO and Co-Founder of Knoldus Inc. Knoldus does niche Reactive and Big Data product development on Scala, Spark, and Functional Java. Knoldus has a strong focus on software craftsmanship which ensures high-quality software development. It partners with the best in the industry like Lightbend (Scala Ecosystem), Databricks (Spark Ecosystem), Confluent (Kafka) and Datastax (Cassandra). Vikas has been working in the cutting edge tech industry for 20+ years. He was an ardent fan of Java with multiple high load enterprise systems to boast of till he met Scala. His current passions include utilizing the power of Scala, Akka and Play to make Reactive and Big Data systems for niche startups and enterprises who would like to change the way software is developed. To know more, send a mail to hello@knoldus.com or visit www.knoldus.com