Reactive Scala Driver for MongoDB
Asynchronous & Non-Blocking
You can setup this serialization pack by adding the following dependency in your
If the dependency for the Play plugin (with the right version) is present, it already provides the JSON support and this JSON serialization pack must not be added as a separate dependency.
Then, the following code enables this JSON serialization pack.
API documentations: Play JSON API
Documents and values
There is one Play JSON class for most of the BSON types, from the
|BSONDBPointer||No JSON type|
|BSONNull||No JSON type|
Using that, any type that can be serialized as JSON can be also be serialized as BSON.
A document is represented by
JsObject, which is basically an immutable list of key-value pairs. Since it is the most used JSON type when working with MongoDB, the ReactiveMongo Play JSON library handles such
JsObjects as seamless as possible. The encoding of such JSON object needs an instance of the typeclass
Writes specialized for object).
This library provides a specialized collection called
JSONCollection that deals naturally with
JsObject. Thanks to it, you can fetch documents from MongoDB in the Play JSON format, transform them by removing and/or adding some properties, and send them to the client.
Even better, when a client sends a JSON document, you can validate it and transform it before saving it into a MongoDB collection (coast-to-coast approach).
The support of Play JSON for ReactiveMongo provides some extensions of the result cursors, as
.jsArray() to read underlying data as a JSON array.
In the previous example, the function
jsAll will return a JSON array containing all the documents of the given collection (as JSON objects).
There are some helpers coming along with the JSON support.
As illustrated by the previous example, the function
Helpers.bulkInsert provides a JSON import feature.
Run a raw command
The command API can be used with the JSON serialization to execution a JSON object as a raw command.
If the following error is raised;
No Json serializer as JsObject found for type play.api.libs.json.JsObject. Try to implement an implicit OWrites or OFormat for this type.
It’s necessary to make sure the right imports are there.