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).
|BSONDBPointer||deprecated in the protocol||None|
|BSONDateTime||UTC Date Time||
|BSONDouble||64-bit IEEE 754 floating point||
|BSONObjectID||12 bytes default id type in MongoDB||None|
|BSONSymbol||deprecated in the protocol||None|
|BSONTimestamp||special date type used in MongoDB internals||None|
|BSONUndefined||deprecated in the protocol||None|
All these classes extend BSONValue.
See how to handle the optional values.
A document is represented by
BSONDocument, which 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 typeclasses whose purpose is to serialize/deserialize objects of arbitrary types into/from BSON. This makes usage of MongoDB much less verbose and more natural.