23 Sep 2015, 13:00

Share

Update - now released!

I’m really pleased to announce the first release candidate of a new MongoDB Scala Driver!

Insider information

At MongoDB we’ve been really busy, back in April we introduced the 3.0 Java driver. It was a massive undertaking that included numerous improvements and updates. What got me most excited with the 3.0 release was the introduction of a new fully asynchronous, non-blocking driver. Using this asynchronous driver as a base we also released an RxJava driver and a Reactive Streams driver.

Today we are announcing a new MongoDB Scala Driver, which also builds upon the asynchronous driver, whilst still providing a first class Scala experience.

Scala specifics

This new Scala driver required much more than a simple wrapping of the Java driver. Here are some of the highlights:

  • A modern idiomatic Scala driver with asynchronous and non-blocking IO.
  • A new Observable type that is both composable and flexible enough to handle streams of data from MongoDB.
  • New immutable and mutable type safe Document classes with all the convenience of a Map.
  • A clean modern API following the latest MongoDB driver specifications.
  • A new namespace for Scala org.mongodb.scala. No more confusion about what classes required for the Scala driver.
  • Comprehensive documentation site to help get you started.
  • Easy integration with other Reactive libraries such as RxScala and Reactive Streams.

Below is a quick example to whet your appetite:

// Connect to the users collection in mydb
val mongoClient: MongoClient = MongoClient()
val database: MongoDatabase = mongoClient.getDatabase("mydb")
val collection: MongoCollection[Document] = database.getCollection("users")

// The Document ADT enforces type safety and can implicitly box native scala types to BSON types
val query = Document("user" -> "Martin")  // "Martin" becomes BsonString("Martin")

// Lets run a query for all Martins and print out the json representation of each document
collection.find(query).subscribe(
  (user: Document) => println(user.toJson()),                         // onNext
  (error: Throwable) => println(s"Query failed: ${error.getMessage}"), // onError
  () => println("Done")                                               // onComplete
)

// Want Futures? No problems!
val futureUsers: Future[Seq[Document]] = collection.find(query).toFuture()

Available on sonatype for Scala 2.11:

"org.mongodb.scala" %% "mongo-scala-driver" % "1.0.0-rc0"

Feedback wanted

We would love to have your feedback on the new driver, so please feel free to email me directly or post to the MongoDB User mailing list.

The best place to get up and running with the new driver is the getting started guide.

comments powered by Disqus