userver: storages::mongo::Pool Class Reference
Loading...
Searching...
No Matches
storages::mongo::Pool Class Reference

#include <userver/storages/mongo/pool.hpp>

Detailed Description

MongoDB client pool.

Use constructor only for tests, in production the pool should be retrieved from the components via components::Mongo::GetPool() or components::MultiMongo::GetPool().

Example usage:

inline void SampleMongoPool(storages::mongo::Pool& pool) {
auto in_coll = pool.GetCollection("aggregate_in");
in_coll.InsertMany({
formats::bson::MakeDoc("_id", 1, "x", 0),
formats::bson::MakeDoc("_id", 2, "x", 1),
formats::bson::MakeDoc("_id", 3, "x", 2),
});
auto cursor = in_coll.Aggregate(
MakeDoc(storages::mongo::operators::kMatch, MakeDoc("_id", MakeDoc(storages::mongo::operators::kGte, 2))),
MakeDoc(storages::mongo::operators::kAddFields, MakeDoc("check", true)),
MakeDoc(storages::mongo::operators::kOut, "aggregate_out")
),
);
EXPECT_FALSE(cursor);
auto out_coll = pool.GetCollection("aggregate_out");
EXPECT_EQ(2, out_coll.CountApprox());
for (const auto& doc : out_coll.Find({})) {
EXPECT_EQ(doc["_id"].As<int>(), doc["x"].As<int>() + 1);
EXPECT_TRUE(doc["check"].As<bool>());
}
}

Definition at line 40 of file pool.hpp.

Public Member Functions

 Pool (Pool &&) noexcept
Pool & operator= (Pool &&) noexcept
bool HasCollection (utils::zstring_view name) const
 Checks whether a collection exists.
Collection GetCollection (std::string name) const
 Returns a handle for the specified collection.
void DropDatabase ()
std::vector< std::string > ListCollectionNames () const
 Get a list of all the collection names in the associated database.
void Ping ()
Transaction BeginTransaction () const
 Begin a new transaction.
template<typename... Options>
Cursor Aggregate (formats::bson::Value pipeline, Options &&... options)
 Executes an aggregation pipeline on the database, without a collection.
Prepared operation executors
Cursor Execute (const operations::Aggregate &)

Member Function Documentation

◆ Aggregate()

template<typename... Options>
Cursor storages::mongo::Pool::Aggregate ( formats::bson::Value pipeline,
Options &&... options )

Executes an aggregation pipeline on the database, without a collection.

Parameters
pipelinean array of aggregation operations
optionssee storages::mongo::options

Corresponds to MongoDB db.aggregate([...]). Use this for pipelines that cannot run on a collection, for example when the first stage is $documents.

On sharded clusters MongoDB may reject $documents together with $lookup: $documents must run on mongos, while $lookup must run on a shard.

See also
Collection::Aggregate
auto cursor =
pool.Aggregate(MakeArray(MakeDoc(mongo::operators::kDocuments, MakeArray(MakeDoc("x", 1), MakeDoc("x", 2)))));
std::vector<int> values;
for (const auto& doc : cursor) {
values.push_back(doc["x"].As<int>());
}

Definition at line 115 of file pool.hpp.

◆ BeginTransaction()

Transaction storages::mongo::Pool::BeginTransaction ( ) const

Begin a new transaction.

Returns
Transaction handle for executing operations within transaction context
Exceptions
MongoExceptionif transaction cannot be started

◆ DropDatabase()

void storages::mongo::Pool::DropDatabase ( )

Drops the associated database if it exists. New modifications of collections will attempt to re-create the database automatically.

◆ Ping()

void storages::mongo::Pool::Ping ( )
Exceptions
storages::mongo::MongoExceptionif failed to connect to the mongo server.

The documentation for this class was generated from the following file: