userver: storages::mongo::Collection Class Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
storages::mongo::Collection Class Reference

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

Detailed Description

MongoDB collection handle, the main way to operate with MongoDB.

Usually retrieved from storages::mongo::Pool

Example:

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("$match", MakeDoc("_id", MakeDoc("$gte", 2))),
MakeDoc("$addFields", MakeDoc("check", true)),
MakeDoc("$out", "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 34 of file collection.hpp.

Public Member Functions

template<typename... Options>
size_t Count (formats::bson::Document filter, Options &&... options) const
 Returns the number of documents matching the query.
 
template<typename... Options>
size_t CountApprox (Options &&... options) const
 Returns an approximated count of all documents in the collection.
 
template<typename... Options>
Cursor Find (formats::bson::Document filter, Options &&... options) const
 Performs a query on the collection.
 
template<typename... Options>
std::optional< formats::bson::DocumentFindOne (formats::bson::Document filter, Options &&... options) const
 Retrieves a single document from the collection.
 
template<typename... Options>
WriteResult InsertOne (formats::bson::Document document, Options &&... options)
 Inserts a single document into the collection.
 
template<typename... Options>
WriteResult InsertMany (std::vector< formats::bson::Document > documents, Options &&... options)
 Inserts multiple documents into the collection.
 
template<typename... Options>
WriteResult ReplaceOne (formats::bson::Document selector, formats::bson::Document replacement, Options &&... options)
 Replaces a single matching document.
 
template<typename... Options>
WriteResult UpdateOne (formats::bson::Document selector, formats::bson::Document update, Options &&... options)
 Updates a single matching document.
 
template<typename... Options>
WriteResult UpdateMany (formats::bson::Document selector, formats::bson::Document update, Options &&... options)
 Updates all matching documents.
 
template<typename... Options>
WriteResult DeleteOne (formats::bson::Document selector, Options &&... options)
 Deletes a single matching document.
 
template<typename... Options>
WriteResult DeleteMany (formats::bson::Document selector, Options &&... options)
 Deletes all matching documents.
 
template<typename... Options>
WriteResult FindAndModify (formats::bson::Document query, const formats::bson::Document &update, Options &&... options)
 Atomically updates a single matching document.
 
template<typename... Options>
WriteResult FindAndRemove (formats::bson::Document query, Options &&... options)
 Atomically removes a single matching document.
 
template<typename... Options>
void Drop (Options &&... options)
 Drop collection.
 
template<typename... Options>
operations::Bulk MakeOrderedBulk (Options &&... options)
 Efficiently executes multiple operations in order, stops on error.
 
template<typename... Options>
operations::Bulk MakeUnorderedBulk (Options &&... options)
 Efficiently executes multiple operations out of order, continues on error.
 
template<typename... Options>
Cursor Aggregate (formats::bson::Value pipeline, Options &&... options)
 Executes an aggregation pipeline.
 
const std::string & GetCollectionName () const
 Get collection name.
 
Prepared operation executors
size_t Execute (const operations::Count &) const
 
size_t Execute (const operations::CountApprox &) const
 
Cursor Execute (const operations::Find &) const
 
WriteResult Execute (const operations::InsertOne &)
 
WriteResult Execute (const operations::InsertMany &)
 
WriteResult Execute (const operations::ReplaceOne &)
 
WriteResult Execute (const operations::Update &)
 
WriteResult Execute (const operations::Delete &)
 
WriteResult Execute (const operations::FindAndModify &)
 
WriteResult Execute (const operations::FindAndRemove &)
 
WriteResult Execute (operations::Bulk &&)
 
Cursor Execute (const operations::Aggregate &)
 
void Execute (const operations::Drop &)
 

Member Function Documentation

◆ Aggregate()

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

Executes an aggregation pipeline.

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

Definition at line 273 of file collection.hpp.

◆ Count()

template<typename... Options>
size_t storages::mongo::Collection::Count ( formats::bson::Document filter,
Options &&... options ) const

Returns the number of documents matching the query.

Warning
Unless explicitly overridden, runs CountApprox for empty filters
See also
options::ForceCountImpl

Definition at line 144 of file collection.hpp.

◆ CountApprox()

template<typename... Options>
size_t storages::mongo::Collection::CountApprox ( Options &&... options) const

Returns an approximated count of all documents in the collection.

Note
This method uses collection metadata and should be faster

Definition at line 151 of file collection.hpp.

◆ DeleteMany()

template<typename... Options>
WriteResult storages::mongo::Collection::DeleteMany ( formats::bson::Document selector,
Options &&... options )

Deletes all matching documents.

Definition at line 230 of file collection.hpp.

◆ DeleteOne()

template<typename... Options>
WriteResult storages::mongo::Collection::DeleteOne ( formats::bson::Document selector,
Options &&... options )

Deletes a single matching document.

Definition at line 223 of file collection.hpp.

◆ Drop()

template<typename... Options>
void storages::mongo::Collection::Drop ( Options &&... options)

Drop collection.

Definition at line 252 of file collection.hpp.

◆ Find()

template<typename... Options>
Cursor storages::mongo::Collection::Find ( formats::bson::Document filter,
Options &&... options ) const

Performs a query on the collection.

Definition at line 168 of file collection.hpp.

◆ FindAndModify()

template<typename... Options>
WriteResult storages::mongo::Collection::FindAndModify ( formats::bson::Document query,
const formats::bson::Document & update,
Options &&... options )

Atomically updates a single matching document.

See also
options::ReturnNew
options::Upsert

Definition at line 238 of file collection.hpp.

◆ FindAndRemove()

template<typename... Options>
WriteResult storages::mongo::Collection::FindAndRemove ( formats::bson::Document query,
Options &&... options )

Atomically removes a single matching document.

Definition at line 245 of file collection.hpp.

◆ FindOne()

template<typename... Options>
std::optional< formats::bson::Document > storages::mongo::Collection::FindOne ( formats::bson::Document filter,
Options &&... options ) const

Retrieves a single document from the collection.

Definition at line 175 of file collection.hpp.

◆ InsertMany()

template<typename... Options>
WriteResult storages::mongo::Collection::InsertMany ( std::vector< formats::bson::Document > documents,
Options &&... options )

Inserts multiple documents into the collection.

Definition at line 192 of file collection.hpp.

◆ InsertOne()

template<typename... Options>
WriteResult storages::mongo::Collection::InsertOne ( formats::bson::Document document,
Options &&... options )

Inserts a single document into the collection.

Definition at line 185 of file collection.hpp.

◆ MakeOrderedBulk()

template<typename... Options>
operations::Bulk storages::mongo::Collection::MakeOrderedBulk ( Options &&... options)

Efficiently executes multiple operations in order, stops on error.

Definition at line 259 of file collection.hpp.

◆ MakeUnorderedBulk()

template<typename... Options>
operations::Bulk storages::mongo::Collection::MakeUnorderedBulk ( Options &&... options)

Efficiently executes multiple operations out of order, continues on error.

Definition at line 266 of file collection.hpp.

◆ ReplaceOne()

template<typename... Options>
WriteResult storages::mongo::Collection::ReplaceOne ( formats::bson::Document selector,
formats::bson::Document replacement,
Options &&... options )

Replaces a single matching document.

See also
options::Upsert

Definition at line 200 of file collection.hpp.

◆ UpdateMany()

template<typename... Options>
WriteResult storages::mongo::Collection::UpdateMany ( formats::bson::Document selector,
formats::bson::Document update,
Options &&... options )

Updates all matching documents.

See also
options::Upsert

Definition at line 216 of file collection.hpp.

◆ UpdateOne()

template<typename... Options>
WriteResult storages::mongo::Collection::UpdateOne ( formats::bson::Document selector,
formats::bson::Document update,
Options &&... options )

Updates a single matching document.

See also
options::Upsert

Definition at line 208 of file collection.hpp.


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