Reactive Scala Driver for MongoDB
Asynchronous & Non-Blocking
Overview of the ReactiveMongo BSON library
The BSON library of ReactiveMongo implements the BSON protocol, or Binary JSON, which is used by MongoDB to encode data. Because of that, when we use MongoDB we tend to manipulate a lot of BSON structures; thus the BSON library is designed with the following points in mind:
- ease of use
- strong typing
Documents and values
There is one Scala class for each BSON type, all in the
BSONDocument: set of key-value pairs
Tfor which a
BSONWriter[T, _]is available can be used as value for a
BSONDocument, as in the
doc2of the previous example (see BSON typeclasses).
BSONArray: sequence of values
BSONDocument, any type with a
BSONWriter(see provided handlers) can be added to a
arr2in the previous example). Moreover, a
Traversable[T]whose element type
BSONWriter[T, _]can be used a BSON array (see
arrFieldin the previous example).
BSONBinary: binary data
BSONDBPointer: deprecated in the protocol
BSONDateTime: UTC Date Time
BSONDouble: 64-bit IEEE 754 floating point
BSONInteger: 32-bit integer
BSONLong: 64-bit integer
BSONMaxKey: max key
BSONMinKey: min key
BSONRegex: regular expression
BSONString: UTF-8 string
BSONSymbol: deprecated in the protocol
BSONTimestamp: special date type used in MongoDB internals
BSONUndefined: deprecated in the protocol
See how to handle the optional values.
All these classes extend BSONValue.
A document is represented by
BSONDocument is basically an immutable list of key-value pairs. Since it is the most used BSON type, one of the main focuses of the ReactiveMongo BSON library is to make manipulations of BSONDocument as easy as possible.
Furthermore, the whole library is articulated around the concept of
These are type classes which purpose is to serialize/deserialize objects of arbitrary types into/from BSON. This makes usage of MongoDB much less verbose and more natural.