11#include <userver/formats/bson/document.hpp>
12#include <userver/formats/bson/value.hpp>
13#include <userver/storages/mongo/bulk.hpp>
14#include <userver/storages/mongo/cursor.hpp>
15#include <userver/storages/mongo/operations.hpp>
16#include <userver/storages/mongo/write_result.hpp>
18USERVER_NAMESPACE_BEGIN
20namespace storages::mongo {
37 explicit Collection(std::shared_ptr<impl::CollectionImpl>);
43 template <
typename... Options>
48 template <
typename... Options>
52 template <
typename... Options>
56 template <
typename... Options>
58 Options&&... options)
const;
61 template <
typename... Options>
65 template <
typename... Options>
67 Options&&... options);
71 template <
typename... Options>
74 Options&&... options);
78 template <
typename... Options>
84 template <
typename... Options>
89 template <
typename... Options>
93 template <
typename... Options>
95 Options&&... options);
100 template <
typename... Options>
103 Options&&... options);
106 template <
typename... Options>
108 Options&&... options);
111 template <
typename... Options>
112 void Drop(Options&&... options);
115 template <
typename... Options>
119 template <
typename... Options>
124 template <
typename... Options>
129 size_t Execute(
const operations::Count&)
const;
130 size_t Execute(
const operations::CountApprox&)
const;
144 std::shared_ptr<impl::CollectionImpl> impl_;
147template <
typename... Options>
151 (count_op.SetOption(std::forward<Options>(options)), ...);
152 return Execute(count_op);
155template <
typename... Options>
158 (count_approx_op.SetOption(std::forward<Options>(options)), ...);
159 return Execute(count_approx_op);
164template <
typename Option,
typename... Options>
165using HasOptionHelper =
166 std::disjunction<std::is_same<std::decay_t<Options>, Option>...>;
168template <
typename Option,
typename... Options>
169static constexpr bool kHasOption = HasOptionHelper<Option, Options...>::value;
173template <
typename... Options>
175 Options&&... options)
const {
177 (find_op.SetOption(std::forward<Options>(options)), ...);
178 return Execute(find_op);
181template <
typename... Options>
183 formats::
bson::
Document filter, Options&&... options)
const {
185 !(std::is_same<std::decay_t<Options>, options::Limit>::value || ...),
186 "Limit option cannot be used in FindOne");
187 auto cursor = Find(std::move(filter), options::Limit{1},
188 std::forward<Options>(options)...);
189 if (cursor.begin() == cursor.end())
return {};
190 return *cursor.begin();
193template <
typename... Options>
195 Options&&... options) {
197 (insert_op.SetOption(std::forward<Options>(options)), ...);
198 return Execute(insert_op);
201template <
typename... Options>
203 std::vector<formats::bson::Document> documents, Options&&... options) {
205 (insert_op.SetOption(std::forward<Options>(options)), ...);
206 return Execute(insert_op);
209template <
typename... Options>
212 Options&&... options) {
214 std::move(replacement));
215 (replace_op.SetOption(std::forward<Options>(options)), ...);
216 return Execute(replace_op);
219template <
typename... Options>
222 Options&&... options) {
224 std::move(selector), std::move(update));
225 (update_op.SetOption(std::forward<Options>(options)), ...);
226 return Execute(update_op);
229template <
typename... Options>
232 Options&&... options) {
234 std::move(selector), std::move(update));
235 (update_op.SetOption(std::forward<Options>(options)), ...);
236 return Execute(update_op);
239template <
typename... Options>
241 Options&&... options) {
243 std::move(selector));
244 (delete_op.SetOption(std::forward<Options>(options)), ...);
245 return Execute(delete_op);
248template <
typename... Options>
250 Options&&... options) {
252 std::move(selector));
253 (delete_op.SetOption(std::forward<Options>(options)), ...);
254 return Execute(delete_op);
257template <
typename... Options>
260 Options&&... options) {
262 (fam_op.SetOption(std::forward<Options>(options)), ...);
263 return Execute(fam_op);
266template <
typename... Options>
268 Options&&... options) {
270 (fam_op.SetOption(std::forward<Options>(options)), ...);
271 return Execute(fam_op);
274template <
typename... Options>
277 (drop_op.SetOption(std::forward<Options>(options)), ...);
278 return Execute(drop_op);
281template <
typename... Options>
284 (bulk.SetOption(std::forward<Options>(options)), ...);
288template <
typename... Options>
291 (bulk.SetOption(std::forward<Options>(options)), ...);
295template <
typename... Options>
297 Options&&... options) {
299 (aggregate.SetOption(std::forward<Options>(options)), ...);
300 return Execute(aggregate);