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>
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>
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 template <typename... Options>
117 Cursor Aggregate(formats::bson::Value pipeline, Options&&... options);
118
119 /// Get collection name
120 const std::string& GetCollectionName() const;
121
122 /// @name Prepared operation executors
123 /// @{
124 size_t Execute(const operations::Count&) const;
125 size_t Execute(const operations::CountApprox&) const;
126 Cursor Execute(const operations::Find&) const;
127 WriteResult Execute(const operations::InsertOne&);
128 WriteResult Execute(const operations::InsertMany&);
129 WriteResult Execute(const operations::ReplaceOne&);
130 WriteResult Execute(const operations::Update&);
131 WriteResult Execute(const operations::Delete&);
132 WriteResult Execute(const operations::FindAndModify&);
133 WriteResult Execute(const operations::FindAndRemove&);
134 WriteResult Execute(operations::Bulk&&);
135 Cursor Execute(const operations::Aggregate&);
136 void Execute(const operations::Drop&);
137 /// @}
138private:
139 std::shared_ptr<impl::CollectionImpl> impl_;
140};
141
142template <typename... Options>
144 operations::Count count_op(std::move(filter));
145 (count_op.SetOption(std::forward<Options>(options)), ...);
146 return Execute(count_op);
147}
148
149template <typename... Options>
151 operations::CountApprox count_approx_op;
152 (count_approx_op.SetOption(std::forward<Options>(options)), ...);
153 return Execute(count_approx_op);
154}
155
156namespace impl {
157
158template <typename Option, typename... Options>
159using HasOptionHelper = std::disjunction<std::is_same<std::decay_t<Options>, Option>...>;
160
161template <typename Option, typename... Options>
162static constexpr bool kHasOption = HasOptionHelper<Option, Options...>::value;
163
164} // namespace impl
165
166template <typename... Options>
167Cursor Collection::Find(formats::bson::Document filter, Options&&... options) const {
168 operations::Find find_op(std::move(filter));
169 (find_op.SetOption(std::forward<Options>(options)), ...);
170 return Execute(find_op);
171}
172
173template <typename... Options>
174std::optional<formats::bson::Document> Collection::FindOne(formats::bson::Document filter, Options&&... options) const {
175 static_assert(
176 !(std::is_same<std::decay_t<Options>, options::Limit>::value || ...), "Limit option cannot be used in FindOne"
177 );
178 auto cursor = Find(std::move(filter), options::Limit{1}, std::forward<Options>(options)...);
179 if (cursor.begin() == cursor.end()) return {};
180 return *cursor.begin();
181}
182
183template <typename... Options>
184WriteResult Collection::InsertOne(formats::bson::Document document, Options&&... options) {
185 operations::InsertOne insert_op(std::move(document));
186 (insert_op.SetOption(std::forward<Options>(options)), ...);
187 return Execute(insert_op);
188}
189
190template <typename... Options>
191WriteResult Collection::InsertMany(std::vector<formats::bson::Document> documents, Options&&... options) {
192 operations::InsertMany insert_op(std::move(documents));
193 (insert_op.SetOption(std::forward<Options>(options)), ...);
194 return Execute(insert_op);
195}
196
197template <typename... Options>
199Collection::ReplaceOne(formats::bson::Document selector, formats::bson::Document replacement, Options&&... options) {
200 operations::ReplaceOne replace_op(std::move(selector), std::move(replacement));
201 (replace_op.SetOption(std::forward<Options>(options)), ...);
202 return Execute(replace_op);
203}
204
205template <typename... Options>
207Collection::UpdateOne(formats::bson::Document selector, formats::bson::Document update, Options&&... options) {
208 operations::Update update_op(operations::Update::Mode::kSingle, std::move(selector), std::move(update));
209 (update_op.SetOption(std::forward<Options>(options)), ...);
210 return Execute(update_op);
211}
212
213template <typename... Options>
215Collection::UpdateMany(formats::bson::Document selector, formats::bson::Document update, Options&&... options) {
216 operations::Update update_op(operations::Update::Mode::kMulti, std::move(selector), std::move(update));
217 (update_op.SetOption(std::forward<Options>(options)), ...);
218 return Execute(update_op);
219}
220
221template <typename... Options>
222WriteResult Collection::DeleteOne(formats::bson::Document selector, Options&&... options) {
223 operations::Delete delete_op(operations::Delete::Mode::kSingle, std::move(selector));
224 (delete_op.SetOption(std::forward<Options>(options)), ...);
225 return Execute(delete_op);
226}
227
228template <typename... Options>
229WriteResult Collection::DeleteMany(formats::bson::Document selector, Options&&... options) {
230 operations::Delete delete_op(operations::Delete::Mode::kMulti, std::move(selector));
231 (delete_op.SetOption(std::forward<Options>(options)), ...);
232 return Execute(delete_op);
233}
234
235template <typename... Options>
237Collection::FindAndModify(formats::bson::Document query, const formats::bson::Document& update, Options&&... options) {
238 operations::FindAndModify fam_op(std::move(query), update);
239 (fam_op.SetOption(std::forward<Options>(options)), ...);
240 return Execute(fam_op);
241}
242
243template <typename... Options>
244WriteResult Collection::FindAndRemove(formats::bson::Document query, Options&&... options) {
245 operations::FindAndRemove fam_op(std::move(query));
246 (fam_op.SetOption(std::forward<Options>(options)), ...);
247 return Execute(fam_op);
248}
249
250template <typename... Options>
251void Collection::Drop(Options&&... options) {
252 operations::Drop drop_op;
253 (drop_op.SetOption(std::forward<Options>(options)), ...);
254 return Execute(drop_op);
255}
256
257template <typename... Options>
258operations::Bulk Collection::MakeOrderedBulk(Options&&... options) {
259 operations::Bulk bulk(operations::Bulk::Mode::kOrdered);
260 (bulk.SetOption(std::forward<Options>(options)), ...);
261 return bulk;
262}
263
264template <typename... Options>
266 operations::Bulk bulk(operations::Bulk::Mode::kUnordered);
267 (bulk.SetOption(std::forward<Options>(options)), ...);
268 return bulk;
269}
270
271template <typename... Options>
272Cursor Collection::Aggregate(formats::bson::Value pipeline, Options&&... options) {
273 operations::Aggregate aggregate(std::move(pipeline));
274 (aggregate.SetOption(std::forward<Options>(options)), ...);
275 return Execute(aggregate);
276}
277
278} // namespace storages::mongo
279
280USERVER_NAMESPACE_END