Reactive Scala Driver for MongoDB
Asynchronous & Non-Blocking
The Collection API is designed to be very extensible, allowing the use of third-party libraries for building documents (e.g. use something else than the embedded BSON library), including specific serializers and deserializers.
This is very useful when you don’t want to explicitly convert your objects into yet another different structure – if your application uses JSON, it is perfectly understandable to want to avoid using BSON only for dealing with MongoDB.
This trait is almost empty.
All collections implementations must mix this trait in. They also provide implicit objects of type
CollectionProducer that make new (specialized) instances of them. Since
db.collection() is parametrized with
C <: Collection and accepts an implicit
CollectionProducer[C], the returned instance of collection can be inferred to the right type if there is only one producer in the implicit scope, which is a typical situation.
Most implementations actually extend another trait,
This trait is much more complete than
Collection. It defines common methods, like
insert(), among others. One particularity of them is that they may be given …
GenericCollection takes 1 type parameter: the
Let’s take an example of how these types are used with
find(), which is defined like this:
This method takes a
selector (or query), of type
S. This object is then transformed into BSON thanks to the implicit
swriter parameter. Moreover, you can notice that the return type is another trait,
GenericQueryBuilder, with the same parameter types.
GenericQueryBuilder, like its name says it, helps building and customizing the query.
- The default implementation in ReactiveMongo,
BSONCollection. It relies on the embedded BSON library, with
BSONDocumentWriteras the de/serializer typeclasses.
- The implementation in the Play JSON support,
JSONCollection. It uses
JsObject(a JSON object), and the de/serializer typeclasses