nilskp / mongolia   0.9.0-201504121559

MIT License GitHub

Scala DSL for MongoDB

Scala versions: 2.11

Build Status Scala version Download Join the chat at https://gitter.im/nilskp/mongolia

Mongolia

Type safe Scala DSL for MongoDB, that has 3 simple goals:

  1. Type-safety. Prevent runtime exceptions by allowing only BSON types or types that can be converted to BSON.
  2. Fix the Java driver's UUID bug in a backwards compatible manner.
  3. Keep syntax close to original Javascript

Basic example:

import mongolia._
import BsonCodecs._

val personId = new ObjectId

val person = obj(
  _id(personId), // equivalent to "_id" := personId
  "name" := obj(
    "first" := "Francis",
    "middle" := "Joseph",
    "last" := "Underwood"
  ),
  "titles" := arr("Majority Whip", "Vice-president", "President")
)

coll.insert(person)

coll.findOpt(_id(personId)) match {
  case None => sys.error(s"Not found: $personId")
  case Some(person) =>
    val firstName: String = person("name.first").as[String]
    val middleName: Option[String] = person("name.middle").opt[String]
    val lastName: String = person("name.last").as[String]
    val titles: List[String] = person("titles").asList[String]