BSONDocumentWriter

BSONDocumentWriter factories.

Companion:
class
class Object
trait Matchable
class Any

Value members

Concrete methods

def apply[T](write: T => BSONDocument): BSONDocumentWriter[T]

Creates a BSONDocumentWriter based on the given write function. This function is called within a scala.util.Try.

Creates a BSONDocumentWriter based on the given write function. This function is called within a scala.util.Try.

import reactivemongo.api.bson.{ BSONDocument, BSONDocumentWriter }

case class Foo(value: String)

val foo: BSONDocumentWriter[Foo] =
 BSONDocumentWriter { (f: Foo) => BSONDocument("value" -> f.value) }

Creates a BSONDocumentWriter based on the given partial function.

Creates a BSONDocumentWriter based on the given partial function.

A exceptions.ValueDoesNotMatchException is returned as Failure for any value that is not matched by the write function.

import reactivemongo.api.bson.{ BSONDocument, BSONDocumentWriter }

case class Foo(value: String)

val writer = BSONDocumentWriter.collect[Foo] {
 case Foo(value) if value.nonEmpty =>
   BSONDocument("value" -> value)
}

'''EXPERIMENTAL:''' Creates a BSONDocumentWriter based on the given partially safe write function.

'''EXPERIMENTAL:''' Creates a BSONDocumentWriter based on the given partially safe write function.

A exceptions.ValueDoesNotMatchException is returned as Failure for any value that is not matched by the write function.

import reactivemongo.api.bson.{ BSONDocument, BSONDocumentWriter }

case class Foo(value: String)

val writer = BSONDocumentWriter.collectFrom[Foo] {
 case Foo(value) if value.nonEmpty =>
   scala.util.Success(BSONDocument("value" -> value))
}
def field[T](name: String)(implicit w: BSONWriter[T]): BSONDocumentWriter[T]

'''EXPERIMENTAL:''' Creates a BSONDocumentWriter that writes a single value as a BSONDocument with a single field.

'''EXPERIMENTAL:''' Creates a BSONDocumentWriter that writes a single value as a BSONDocument with a single field.

Value parameters:
name

the name of the field to be written

import reactivemongo.api.bson.BSONDocumentWriter
val writer = BSONDocumentWriter.field[String]("foo")
writer.writeTry("bar")
// => Success: {'foo': 'bar'}
def from[T](write: T => Try[BSONDocument]): BSONDocumentWriter[T]

Creates a BSONDocumentWriter based on the given write safe function.

Creates a BSONDocumentWriter based on the given write safe function.

import scala.util.Success
import reactivemongo.api.bson.{ BSONDocument, BSONDocumentWriter }

case class Foo(value: String)

val writer = BSONDocumentWriter.from[Foo] { foo =>
 Success(BSONDocument("value" -> foo.value))
}
Value parameters:
write

the safe function to write T values as BSON

def option[T](write: T => Option[BSONDocument]): BSONDocumentWriter[T]

Creates a BSONWriter based on the given write function.

Creates a BSONWriter based on the given write function.

import reactivemongo.api.bson.{ BSONDocument, BSONDocumentWriter }

case class Foo(value: String)

val writer = BSONDocumentWriter.option[Foo] { foo =>
 Some(BSONDocument("value" -> foo.value))
}
def tuple2[A : BSONWriter, B : BSONWriter](field1: String, field2: String): BSONDocumentWriter[(A, B)]

'''EXPERIMENTAL:''' Creates a BSONDocumentWriter that writes tuple elements as BSONDocument fields.

'''EXPERIMENTAL:''' Creates a BSONDocumentWriter that writes tuple elements as BSONDocument fields.

import reactivemongo.api.bson.BSONDocumentWriter

val writer = BSONDocumentWriter.tuple2[String, Int]("name", "age")

writer.writeTry("Foo" -> 20)
// => Success: {'name': 'Foo', 'age': 20}
def tuple3[A : BSONWriter, B : BSONWriter, C : BSONWriter](field1: String, field2: String, field3: String): BSONDocumentWriter[(A, B, C)]

'''EXPERIMENTAL:''' Creates a BSONDocumentWriter that writes tuple elements as BSONDocument fields.

'''EXPERIMENTAL:''' Creates a BSONDocumentWriter that writes tuple elements as BSONDocument fields.

def tuple4[A : BSONWriter, B : BSONWriter, C : BSONWriter, D : BSONWriter](field1: String, field2: String, field3: String, field4: String): BSONDocumentWriter[(A, B, C, D)]

'''EXPERIMENTAL:''' Creates a BSONDocumentWriter that writes tuple elements as BSONDocument fields.

'''EXPERIMENTAL:''' Creates a BSONDocumentWriter that writes tuple elements as BSONDocument fields.

def tuple5[A : BSONWriter, B : BSONWriter, C : BSONWriter, D : BSONWriter, E : BSONWriter](field1: String, field2: String, field3: String, field4: String, field5: String): BSONDocumentWriter[(A, B, C, D, E)]

'''EXPERIMENTAL:''' Creates a BSONDocumentWriter that writes tuple elements as BSONDocument fields.

'''EXPERIMENTAL:''' Creates a BSONDocumentWriter that writes tuple elements as BSONDocument fields.