A BSONArray
(type 0x04
) is a indexed sequence of BSONValue.
A BSONArray
(type 0x04
) is a indexed sequence of BSONValue.
import reactivemongo.api.bson._ BSONArray(BSONString("foo"), BSONDouble(1.2D))
A BSON binary value.
A BSON binary value.
import reactivemongo.api.bson.{ BSONBinary, Subtype } BSONBinary("foo".getBytes("UTF-8"), Subtype.GenericBinarySubtype)
BSON boolean value
A BSON value that can be seen as a boolean.
A BSON value that can be seen as a boolean.
Conversions:
number = 0 ~> false
number != 0 ~> true
boolean
undefined ~> false
null ~> false
import scala.util.Success import reactivemongo.api.bson.{ BSONBooleanLike, BSONDocument, BSONInteger } val bi = BSONInteger(1) assert(bi.asTry[BSONBooleanLike].flatMap(_.toBoolean) == Success(true)) val doc = BSONDocument("field" -> bi) assert(doc.getAsTry[BSONBooleanLike]("field"). flatMap(_.toBoolean) == Success(true))
BSON date time value
Value wrapper for a BSON 128-bit decimal.
A BSONDocument
structure (BSON type 0x03
).
A BSONDocument
structure (BSON type 0x03
).
A BSONDocument
is an unordered set of fields (String, BSONValue)
.
Note: The insertion/initial order of the fields may not be maintained through the operations.
Reads and writers T
values to/from BSONDocument.
BSONReader specialized for BSONDocument
BSONWriter specialized for BSONDocument
A BSON Double.
A BSON Double.
reactivemongo.api.bson.BSONDouble(1.23D)
BSON element, typically a pair of name and BSONValue.
BSON element, typically a pair of name and BSONValue.
import reactivemongo.api.bson.{ BSONElement, BSONString } BSONElement("name", BSONString("value"))
A BSON handler is able to both read and write T
values
from/to BSON representation.
A BSON handler is able to both read and write T
values
from/to BSON representation.
import scala.util.Try import reactivemongo.api.bson.{ BSONHandler, BSONValue } def roundtrip[T](value: T)(implicit handler: BSONHandler[T]): Try[Boolean] = for { bson: BSONValue <- handler.writeTry(value) dser <- handler.readTry(bson) } yield (dser == value) // true
BSON Integer value
BSON JavaScript value.
BSON JavaScript value with scope (WS).
BSON Long value
BSON Max key value
BSON Min key value
BSON null value
A BSON value that can be seen as a number.
A BSON value that can be seen as a number.
Conversions:
.value
)import scala.util.Success import reactivemongo.api.bson.{ BSONNumberLike, BSONDocument, BSONInteger } val bi = BSONInteger(1) assert(bi.asTry[BSONNumberLike].flatMap(_.toLong) == Success(1L)) val doc = BSONDocument("field" -> bi) assert(doc.getAsTry[BSONNumberLike]("field"). flatMap(_.toDouble) == Success(1D))
BSON ObjectId value.
BSON ObjectId value.
import scala.util.Try import reactivemongo.api.bson.BSONObjectID val oid: BSONObjectID = BSONObjectID.generate() def foo: Try[BSONObjectID] = BSONObjectID.parse(oid.stringify)
| Timestamp (seconds) | Machine identifier | Thread identifier | Increment | --- | --- | --- | --- | 4 bytes | 3 bytes | 2 bytes | 3 bytes
A reader that produces an instance of T
from a subtype of BSONValue.
BSON Regex value.
EXPERIMENTAL: Strict documentation representation with at most one value per field name (no duplicate).
EXPERIMENTAL: Strict documentation representation with at most one value per field name (no duplicate).
import reactivemongo.api.bson.BSONDocument def strict1 = // { 'foo': 1 } BSONDocument.strict("foo" -> 1, "foo" -> 2) def strict2 = BSONDocument("foo" -> 1, "foo" -> 2).asStrict assert(strict1 == strict2)
A BSON string.
A BSON string.
reactivemongo.api.bson.BSONString("foo")
BSON Symbol value.
BSON Timestamp value
BSON undefined value
A BSON value
A writer that produces a subtype of BSONValue from an instance of T
.
Evidence that T
can be serialized as a BSON document.
Evidence that T
can be serialized as a BSON document.
See BSONDocument
Naming strategy, to map each class property to the corresponding field.
Naming strategy, to map each class property to the corresponding field.
import reactivemongo.api.bson.{ FieldNaming, MacroConfiguration } def initCfg(naming: FieldNaming): MacroConfiguration = MacroConfiguration(fieldNaming = naming)
Mapping from a BSON string to T
;
Used by scala.collection.Map handlers.
Mapping from a BSON string to T
;
Used by scala.collection.Map handlers.
final class Foo(val v: String) extends AnyVal val bson = reactivemongo.api.bson.BSONDocument( "foo:key" -> 1, "foo:name" -> 2) import reactivemongo.api.bson.KeyReader implicit def fooKeyReader: KeyReader[Foo] = KeyReader[Foo] { str => new Foo(str.stripPrefix("foo:")) } reactivemongo.api.bson.BSON.readDocument(bson) // Success: Map[Foo, Int]( // (new Foo("key") -> 1), // (new Foo("name") -> 2))
Mapping from a BSON string to T
.
Mapping from a BSON string to T
.
Used by scala.collection.Map handlers.
final class Foo(val v: String) extends AnyVal val dict = Map[Foo, Int]( (new Foo("key") -> 1), (new Foo("name") -> 2)) import reactivemongo.api.bson.KeyWriter implicit def fooKeyWriter: KeyWriter[Foo] = KeyWriter[Foo] { foo => "foo:" + foo.v } reactivemongo.api.bson.BSON.writeDocument(dict) // Success = {'foo:key': 1, 'foo:name': 2}
Macro configuration;
Macro configuration;
It allows to configure compile time options, and behaviour to be retained at runtime (field & type naming).
import reactivemongo.api.bson.{ BSONDocumentReader, MacroConfiguration, Macros } case class Foo(name: String) val r1: BSONDocumentReader[Foo] = Macros.configured.reader[Foo] val r2: BSONDocumentReader[Foo] = Macros.configured( MacroConfiguration.simpleTypeName).reader[Foo]
Macros with 'Opts' suffix will take additional options in the form of type parameters that will customize behaviour of the macros during compilation.
Macros with 'Opts' suffix will take additional options in the form of type parameters that will customize behaviour of the macros during compilation.
import reactivemongo.api.bson.{ BSONDocumentWriter, Macros, MacroOptions } case class Bar(score: Float) val w: BSONDocumentWriter[Bar] = Macros.using[MacroOptions.Default].writer[Bar]
Binary Subtype
Naming strategy, to map each class to a discriminator value.
Naming strategy, to map each class to a discriminator value.
import reactivemongo.api.bson.{ MacroConfiguration, TypeNaming } val cfg1 = MacroConfiguration(typeNaming = TypeNaming.FullName) val cfg2 = MacroConfiguration(typeNaming = TypeNaming.FullName) val cfg3 = MacroConfiguration( typeNaming = TypeNaming { cls: Class[_] => "_" + cls.getSimpleName })
Type level evidence that type A
is not type B
.
Type level evidence that type A
is not type B
.
Utility functions
BSONArray utilities
BSONArray utilities
import reactivemongo.api.bson.{ BSONArray, BSONString } BSONArray("foo", 1) match { case BSONArray(BSONString(s) +: _) => s == "foo" case _ => false }
BSONBinary utilities
BSONBinary utilities
import reactivemongo.api.bson.{ BSONBinary, Subtype } val bin1 = BSONBinary( "foo".getBytes("UTF-8"), Subtype.GenericBinarySubtype) // See Subtype.UuidSubtype val uuid = BSONBinary(java.util.UUID.randomUUID())
BSONBoolean factories & utilities
BSONBooleanLike utilities
BSONDateTime factories & utilities
BSONDecimal factories & utilities
BSONDocument factories & utilities.
BSONDocument factories & utilities.
reactivemongo.api.bson.BSONDocument("foo" -> 1, "bar" -> "lorem")
BSONDocumentHandler factories
BSONDocumentReader factories
BSONDocumentWriter factories.
BSONDouble utilities
BSONDouble utilities
import reactivemongo.api.bson.BSONDouble BSONDouble(1.23D) match { case BSONDouble(v) => assert(v == 1.23D) case _ => ??? }
BSONElement factories and utilities.
BSONHandler factories
BSONInteger factories & utilities
BSONJavaScript factories & utilities
BSONJavaScriptWS factories & utilities
BSONLong factories & utilities
BSONNumberLike utilities
BSONObjectID utilities
BSONReader factories
BSONRegex factories & utilities
BSONString utilities
BSONString utilities
import reactivemongo.api.bson.BSONString BSONString("foo") match { case BSONString(v) => v == "foo" case _ => false }
BSONSymbol factories & utilities
Timestamp companion
Single value for BSONUndefined type
BSONValue factories and utilities
BSONWriter factories.
See DocumentClass
Naming companion
KeyWriter factories
MacroConfiguration factories and utilities
MacroOptions factories & utilities.
Macros for generating BSONReader
and BSONWriter
at compile time.
Macros for generating BSONReader
and BSONWriter
at compile time.
import reactivemongo.api.bson.Macros case class Person(name: String, surname: String) implicit val personHandler = Macros.handler[Person]
MacroConfiguration for extended configuration
MacroOptions for specific options
TypeNaming factories
Returns an array with given values.
Returns an array with given values.
import reactivemongo.api.bson._ val arr = array("bar", 1L) // [ 'bar', NumberLong(1) ]
Returns an empty array.
Returns an empty array.
import reactivemongo.api.bson._ val arr1 = BSONString("bar") +: array // [ 'bar' ] val arr2 = BSONInteger(1) +: arr1 // [ 1, 'bar' ]
Returns a BSON handler for java.time.LocalDate
,
considering the specified time zone
.
Returns a BSON handler for java.time.LocalDate
,
considering the specified time zone
.
Returns a BSON handler for java.time.LocalDateTime
,
considering the specified time zone
.
Returns a BSON handler for java.time.LocalDateTime
,
considering the specified time zone
.
Returns a BSON handler for java.time.OffsetDateTime
,
considering the specified time zone
.
Returns a BSON handler for java.time.OffsetDateTime
,
considering the specified time zone
.
Returns a BSON handler for java.time.ZonedDateTime
,
considering the specified time zone
.
Returns a BSON handler for java.time.ZonedDateTime
,
considering the specified time zone
.
Returns a document with given elements.
Returns a document with given elements.
import reactivemongo.api.bson._ val doc = document("foo" -> 1) // { 'foo': 1 }
Returns an empty document.
Returns an empty document.
import reactivemongo.api.bson._ val doc = document ++ ("foo" -> 1) // { 'foo': 1 }
Returns a newly generated object ID.
Returns a BSON MaxKey value
Keeps a A
statement but raise a migration error at compile-time.
Keeps a A
statement but raise a migration error at compile-time.
The compilation error can be disabled by setting the system property
reactivemongo.api.migrationRequired.nonFatal
to true
.
Returns a BSON MinKey value
Key/value ordering
Key/value ordering
import reactivemongo.api.bson.BSONString Seq("foo" -> BSONString("1"), "bar" -> BSONString("1")). sorted // == [ "bar" -> .., "foo" -> .. ]
Returns a BSON Null value
Returns a BSON Undefined value
BSON main API
System properties:
The following properties can be set (e.g. using JVM
-D
options).reactivemongo.api.bson.bufferSizeBytes
(integer; default:96
): Number of bytes used as initial size when allocating a new buffer.reactivemongo.api.bson.document.strict
(boolean; default:false
): Flag to enable strict reading of document (filter duplicate fields, see BSONDocument.asStrict).