active channel in connection pool • Not sure the DB reference is immediately usable import reactivemongo.api.{ DB, MongoConnection } def deprecatedDB(con: MongoConnection) = con.db("db_name")
utilities for WriteResult • WriteResult.Code: match by error code • WriteResult.Message: match by error message import reactivemongo.api.commands.WriteResult def example6(res: WriteResult) = res match { case WriteResult.Code(11000) => println("Match the code 11000") // 11000 code for the Duplicate error case WriteResult.Message( "Must match this exact message") => println("Match the error message") case _ => () }
utilities for CommandError • CommandError.Code: match by error code • CommandError.Message: match by error message import reactivemongo.api.commands.CommandError def example7(res: CommandError) = res match { case CommandError.Code(11000) => println("Match the code 11000") case CommandError.Message( "Must match this exact message") => println("Match the error message") case _ => () }
Reader for MyEnum based on the one for BSON string import reactivemongo.bson._ sealed trait MyEnum object EnumValA extends MyEnum object EnumValB extends MyEnum val underlying: implicitly[BSONReader[BSONString, String]] implicit def example9: BSONReader[BSONString, MyEnum] = underlying.afterRead { case "A" => EnumValA case "B" => EnumValB case v => sys.error(s"unexpected value: $v") }
Writer to encode as BSON string the instances of MyEnum import reactivemongo.bson._ val underlying = implicitly[BSONWriter[String, BSONString]] implicit def example10: BSONWriter[MyEnum, BSONString] = underlying.beforeWrite[MyEnum] { case EnumValA => "A" case _ => "B" }
Example: Types family of Node and Leaf import reactivemongo.bson._ sealed trait Tree case class Node(left: Tree, right: Tree) extends Tree case class Leaf(data: String) extends Tree val example11: BSONHandler[BSONDocument, Tree] = Macros.handler[Tree]
as collection operation Example: Find the distinct state from a collection of cities import scala.concurrent.Future import scala.concurrent.ExecutionContext.Implicits.global import reactivemongo.bson.BSONDocument import reactivemongo.api.collections.bson.BSONCollection def example14(cities: BSONCollection): Future[Set[String]] = cities.distinct[String, Set]("state")
Different polls can be injected: import javax.inject.Inject import play.modules.reactivemongo._ class MyComponent @Inject() ( val defaultApi: ReactiveMongoApi, // for 'mongodb.uri' @NamedDatabase("bar") val barApi: ReactiveMongoApi // for 'mongodb.bar' ) { /* ... */ } # The default URI mongodb.uri = "mongodb://someuser:somepasswd@localhost:27017/foo" # Another one, named with 'bar' mongodb.bar.uri = "mongodb://someuser:somepasswd@localhost:27017/lorem"