userver: userver/storages/mongo/collection.hpp Source File
Loading...
Searching...
No Matches
collection.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/mongo/collection.hpp
4/// @brief @copybrief storages::mongo::Collection
5
6#include <memory>
7#include <optional>
8#include <string>
9#include <type_traits>
10#include <vector>
11
12#include <userver/formats/bson/document.hpp>
13#include <userver/formats/bson/value.hpp>
14#include <userver/storages/mongo/bulk.hpp>
15#include <userver/storages/mongo/cursor.hpp>
16#include <userver/storages/mongo/operations.hpp>
17#include <userver/storages/mongo/write_result.hpp>
18
19USERVER_NAMESPACE_BEGIN
20
21namespace storages::mongo {
22
23namespace impl {
24class CollectionImpl;
25} // namespace impl
26
27/// @brief MongoDB collection handle, the main way to operate with MongoDB.
28///
29/// Usually retrieved from storages::mongo::Pool
30///
31/// ## Example:
32///
33/// @snippet storages/mongo/collection_mongotest.hpp Sample Mongo usage
35public:
36 /// @cond
37 // For internal use only.
38 explicit Collection(std::shared_ptr<impl::CollectionImpl>);
39 /// @endcond
40
41 /// @brief Returns the number of documents matching the query
42 /// @warning Unless explicitly overridden, runs CountApprox for empty filters
43 /// @see options::ForceCountImpl
44 template <typename... Options>
45 size_t Count(formats::bson::Document filter, Options&&... options) const;
46
47 /// @brief Returns an approximated count of all documents in the collection
48 /// @note This method uses collection metadata and should be faster
49 template <typename... Options>
50 size_t CountApprox(Options&&... options) const;
51
52 /// Performs a query on the collection
53 template <typename... Options>
54 Cursor Find(formats::bson::Document filter, Options&&... options) const;
55
56 /// Retrieves a single document from the collection
57 template <typename... Options>
58 std::optional<formats::bson::Document> FindOne(formats::bson::Document filter, Options&&... options) const;
59
60 /// Inserts a single document into the collection
61 template <typename... Options>
62 WriteResult InsertOne(formats::bson::Document document, Options&&... options);
63
64 /// Inserts multiple documents into the collection
65 template <typename... Options>
66 WriteResult InsertMany(std::vector<formats::bson::Document> documents, Options&&... options);
67
68 /// @brief Replaces a single matching document
69 /// @see options::Upsert
70 template <typename... Options>
71 WriteResult ReplaceOne(formats::bson::Document selector, formats::bson::Document replacement, Options&&... options);
72
73 /// @brief Updates a single matching document
74 /// @see options::Upsert
75 template <typename... Options>
76 WriteResult UpdateOne(formats::bson::Document selector, formats::bson::Document update, Options&&... options);
77
78 /// @brief Updates all matching documents
79 /// @see options::Upsert
80 template <typename... Options>
81 WriteResult UpdateMany(formats::bson::Document selector, formats::bson::Document update, Options&&... options);
82
83 /// Deletes a single matching document
84 template <typename... Options>
85 WriteResult DeleteOne(formats::bson::Document selector, Options&&... options);
86
87 /// Deletes all matching documents
88 template <typename... Options>
89 WriteResult DeleteMany(formats::bson::Document selector, Options&&... options);
90
91 /// @brief Atomically updates a single matching document
92 /// @see options::ReturnNew
93 /// @see options::Upsert
94 template <typename... Options>
96 FindAndModify(formats::bson::Document query, const formats::bson::Document& update, Options&&... options);
97
98 /// Atomically removes a single matching document
99 template <typename... Options>
100 WriteResult FindAndRemove(formats::bson::Document query, Options&&... options);
101
102 /// Drop collection
103 template <typename... Options>
104 void Drop(Options&&... options);
105
106 /// Efficiently executes multiple operations in order, stops on error
107 template <typename... Options>
108 operations::Bulk MakeOrderedBulk(Options&&... options);
109
110 /// Efficiently executes multiple operations out of order, continues on error
111 template <typename... Options>
112 operations::Bulk MakeUnorderedBulk(Options&&... options);
113
114 /// @brief Executes an aggregation pipeline
115 /// @param pipeline an array of aggregation operations
116 /// @param options see @ref storages::mongo::options
117 template <typename... Options>
118 Cursor Aggregate(formats::bson::Value pipeline, Options&&... options);
119
120 /// @brief Retrieves distinct values for a specified field
121 /// @param field name of the field for which to return distinct values
122 /// @param options see @ref storages::mongo::options
123 template <typename... Options>
124 std::vector<formats::bson::Value> Distinct(std::string field, Options&&... options) const;
125
126 /// @brief Retrieves distinct values for a specified field with a query filter
127 /// @param field name of the field for which to return distinct values
128 /// @param filter query that specifies the documents from which to retrieve distinct values
129 /// @param options see @ref storages::mongo::options
130 template <typename... Options>
131 std::vector<formats::bson::Value> Distinct(std::string field, formats::bson::Document filter, Options&&... options)
132 const;
133
134 /// Get collection name
135 const std::string& GetCollectionName() const;
136
137 /// @name Prepared operation executors
138 /// @{
139 size_t Execute(const operations::Count&) const;
140 size_t Execute(const operations::CountApprox&) const;
141 Cursor Execute(const operations::Find&) const;
142 std::vector<formats::bson::Value> Execute(const operations::Distinct&) const;
143 WriteResult Execute(const operations::InsertOne&);
144 WriteResult Execute(const operations::InsertMany&);
145 WriteResult Execute(const operations::ReplaceOne&);
146 WriteResult Execute(const operations::Update&);
147 WriteResult Execute(const operations::Delete&);
148 WriteResult Execute(const operations::FindAndModify&);
149 WriteResult Execute(const operations::FindAndRemove&);
150 WriteResult Execute(operations::Bulk&&);
151 Cursor Execute(const operations::Aggregate&);
152 void Execute(const operations::Drop&);
153 /// @}
154private:
155 std::shared_ptr<impl::CollectionImpl> impl_;
156};
157
158template <typename... Options>
159size_t Collection::Count(formats::bson::Document filter, Options&&... options) const {
160 operations::Count count_op(std::move(filter));
161 (count_op.SetOption(std::forward<Options>(options)), ...);
162 return Execute(count_op);
163}
164
165template <typename... Options>
166size_t Collection::CountApprox(Options&&... options) const {
167 operations::CountApprox count_approx_op;
168 (count_approx_op.SetOption(std::forward<Options>(options)), ...);
169 return Execute(count_approx_op);
170}
171
172namespace impl {
173
174template <typename Option, typename... Options>
175using HasOptionHelper = std::disjunction<std::is_same<std::decay_t<Options>, Option>...>;
176
177template <typename Option, typename... Options>
178static constexpr bool kHasOption = HasOptionHelper<Option, Options...>::value;
179
180} // namespace impl
181
182template <typename... Options>
183Cursor Collection::Find(formats::bson::Document filter, Options&&... options) const {
184 operations::Find find_op(std::move(filter));
185 (find_op.SetOption(std::forward<Options>(options)), ...);
186 return Execute(find_op);
187}
188
189template <typename... Options>
190std::optional<formats::bson::Document> Collection::FindOne(formats::bson::Document filter, Options&&... options) const {
191 static_assert(
192 !(std::is_same<std::decay_t<Options>, options::Limit>::value || ...), "Limit option cannot be used in FindOne"
193 );
194 auto cursor = Find(std::move(filter), options::Limit{1}, std::forward<Options>(options)...);
195 if (cursor.begin() == cursor.end()) return {};
196 return *cursor.begin();
197}
198
199template <typename... Options>
200WriteResult Collection::InsertOne(formats::bson::Document document, Options&&... options) {
201 operations::InsertOne insert_op(std::move(document));
202 (insert_op.SetOption(std::forward<Options>(options)), ...);
203 return Execute(insert_op);
204}
205
206template <typename... Options>
207WriteResult Collection::InsertMany(std::vector<formats::bson::Document> documents, Options&&... options) {
208 operations::InsertMany insert_op(std::move(documents));
209 (insert_op.SetOption(std::forward<Options>(options)), ...);
210 return Execute(insert_op);
211}
212
213template <typename... Options>
215Collection::ReplaceOne(formats::bson::Document selector, formats::bson::Document replacement, Options&&... options) {
216 operations::ReplaceOne replace_op(std::move(selector), std::move(replacement));
217 (replace_op.SetOption(std::forward<Options>(options)), ...);
218 return Execute(replace_op);
219}
220
221template <typename... Options>
223Collection::UpdateOne(formats::bson::Document selector, formats::bson::Document update, Options&&... options) {
224 operations::Update update_op(operations::Update::Mode::kSingle, std::move(selector), std::move(update));
225 (update_op.SetOption(std::forward<Options>(options)), ...);
226 return Execute(update_op);
227}
228
229template <typename... Options>
231Collection::UpdateMany(formats::bson::Document selector, formats::bson::Document update, Options&&... options) {
232 operations::Update update_op(operations::Update::Mode::kMulti, std::move(selector), std::move(update));
233 (update_op.SetOption(std::forward<Options>(options)), ...);
234 return Execute(update_op);
235}
236
237template <typename... Options>
238WriteResult Collection::DeleteOne(formats::bson::Document selector, Options&&... options) {
239 operations::Delete delete_op(operations::Delete::Mode::kSingle, std::move(selector));
240 (delete_op.SetOption(std::forward<Options>(options)), ...);
241 return Execute(delete_op);
242}
243
244template <typename... Options>
245WriteResult Collection::DeleteMany(formats::bson::Document selector, Options&&... options) {
246 operations::Delete delete_op(operations::Delete::Mode::kMulti, std::move(selector));
247 (delete_op.SetOption(std::forward<Options>(options)), ...);
248 return Execute(delete_op);
249}
250
251template <typename... Options>
253Collection::FindAndModify(formats::bson::Document query, const formats::bson::Document& update, Options&&... options) {
254 operations::FindAndModify fam_op(std::move(query), update);
255 (fam_op.SetOption(std::forward<Options>(options)), ...);
256 return Execute(fam_op);
257}
258
259template <typename... Options>
260WriteResult Collection::FindAndRemove(formats::bson::Document query, Options&&... options) {
261 operations::FindAndRemove fam_op(std::move(query));
262 (fam_op.SetOption(std::forward<Options>(options)), ...);
263 return Execute(fam_op);
264}
265
266template <typename... Options>
267void Collection::Drop(Options&&... options) {
268 operations::Drop drop_op;
269 (drop_op.SetOption(std::forward<Options>(options)), ...);
270 return Execute(drop_op);
271}
272
273template <typename... Options>
274operations::Bulk Collection::MakeOrderedBulk(Options&&... options) {
275 operations::Bulk bulk(operations::Bulk::Mode::kOrdered);
276 (bulk.SetOption(std::forward<Options>(options)), ...);
277 return bulk;
278}
279
280template <typename... Options>
282 operations::Bulk bulk(operations::Bulk::Mode::kUnordered);
283 (bulk.SetOption(std::forward<Options>(options)), ...);
284 return bulk;
285}
286
287template <typename... Options>
288Cursor Collection::Aggregate(formats::bson::Value pipeline, Options&&... options) {
289 operations::Aggregate aggregate(std::move(pipeline));
290 (aggregate.SetOption(std::forward<Options>(options)), ...);
291 return Execute(aggregate);
292}
293
294template <typename... Options>
295std::vector<formats::bson::Value> Collection::Distinct(std::string field, Options&&... options) const {
296 operations::Distinct distinct_op(std::move(field));
297 (distinct_op.SetOption(std::forward<Options>(options)), ...);
298 return Execute(distinct_op);
299}
300
301template <typename... Options>
302std::vector<formats::bson::Value>
303Collection::Distinct(std::string field, formats::bson::Document filter, Options&&... options) const {
304 operations::Distinct distinct_op(std::move(field), std::move(filter));
305 (distinct_op.SetOption(std::forward<Options>(options)), ...);
306 return Execute(distinct_op);
307}
308
309} // namespace storages::mongo
310
311USERVER_NAMESPACE_END