The mongo asynchronous driver provides an interface to work with MongoDB databases and the BSON format.
Most important ones:
Metric name | Description |
---|---|
mongo.pool.current-size | current number of open connections |
mongo.pool.max-size | limit on the number of open connections |
mongo.pool.overloads | counter of requests that could not get a connection |
mongo.pool.queue-wait-timings | waiting timings in the queue to receive a connection |
mongo.pool.conn-request-timings | connection receipt timings (includes queue-wait) |
mongo.pool.conn-created/closed | open/closed connection counters |
mongo.success | counter of successfully executed requests |
mongo.errors | counter of failed requests |
mongo.timings | query timings |
See Service Statistics and Metrics (Prometheus/Graphite/...) for info on how to get the metrics.
To use MongoDB you have to add the component components::Mongo and configure it according to the documentation. After that you can work with a collection via storages::mongo::Collection:
Methods and options correspond to the standard MongoDB Collections API.
It is recommended to set timeouts on the server side using storages::mongo::options::MaxServerTime as for other cancellation mechanisms MongoDB does not guarantee that the cancelled operation was not applied.
For client-side timeouts consider setting socket and/or connection timeouts in pool configuration as native task cancellation is not supported in the current implementation.
Methods that modify a collection return an object storages::mongo::WriteResult containing information about the result of the operation:
In cases where the same operation is performed with the same arguments many times, it can be stored into a variable and reused. The finished operation can be passed to the collection method Execute()
.
BSON follows the common formats interface that is described in detail at Formats (JSON, YAML, BSON, ...).
Since BSON is a binary format, it is not human readable. Differences in the BSON and JSON formats type systems do not always allow you to perform conversions between them unambiguously. In this regard, we do not provide conversion functions between BSON and text formats by default, but they are available in <userver/formats/bson/serialize.hpp>. These functions are provided without guarantees for the stability of the conversion, and are primarily intended for debugging.
Extra database load may lead to database overload. To protect Mongo from this happening userver has Congestion Control. In case of fast timings increase, the Congestion Control limits the number of concurrent connections to Mongo and eliminates the extra load. On the one hand, it leads to extra error responses, but on the other hand, it greatly lowers the timings of the rest of the database requests and helps Mongo to return to the normal state with lower response timings.