20 Oct 2015, 13:10

Share

The new Scala Driver for MongoDB has been released!

Last month I announced the first release candidate of a new idiomatic Scala Driver for MongoDB and I’m excited to announce that the first official release is now available on sonatype for Scala 2.11:

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

A clean, simple Scala driver.

At MongoDB we’ve been listening to your feedback about what you would like from a new Scala driver. With that in mind we’ve written a totally new Scala driver. Here are some of the highlights:

  • A modern idiomatic Scala driver with asynchronous and non-blocking IO.
  • 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 Scala.
  • 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.
  • 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 martin = Document("user" -> "Martin")  // "Martin" becomes BsonString("Martin")

// Alternatively, create Documents from Json
val query = Document("""{user: "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()
val firstMartin: Future[Document] = collection.find(query).first().head()

More examples and full documentation can be found on the documentation hub, including a full getting started guide.

Feedback wanted

We would love to have your feedback on the new driver, so please feel free to post to the MongoDB User mailing list or add feature requests to the Jira project. There are a number of items on the roadmap such as; MongoDB Server 3.2 and Case Class support but all feature requests are welcome.

Enjoy!

comments powered by Disqus