userver
C++ Async Framework
Loading...
Searching...
No Matches
options.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/storages/mongo/options.hpp
4
/// @brief Query options
5
6
#
include
<
chrono
>
7
#
include
<
cstddef
>
8
#
include
<
cstdint
>
9
#
include
<
initializer_list
>
10
#
include
<
optional
>
11
#
include
<
string
>
12
#
include
<
string_view
>
13
#
include
<
utility
>
14
#
include
<
vector
>
15
16
#
include
<
userver
/
formats
/
bson
/
bson_builder
.
hpp
>
17
#
include
<
userver
/
formats
/
bson
/
document
.
hpp
>
18
#
include
<
userver
/
formats
/
bson
/
value
.
hpp
>
19
#
include
<
userver
/
formats
/
bson
/
value_builder
.
hpp
>
20
#
include
<
userver
/
formats
/
common
/
type
.
hpp
>
21
22
USERVER_NAMESPACE_BEGIN
23
24
/// Collection operation options
25
namespace
storages::mongo::
options
{
26
27
/// @brief Read preference
28
/// @see https://github.com/mongodb/mongo-c-driver/blob/master/src/libmongoc/doc/mongoc_read_prefs_t.rst
29
class
ReadPreference
{
30
public
:
31
enum
Mode
{
32
/// Default mode. All operations read from the current replica set primary.
33
kPrimary
,
34
/// All operations read from among the nearest secondary members of the replica set.
35
kSecondary
,
36
/// In most situations, operations read from the primary but if it is unavailable, operations read from
37
/// secondary members.
38
kPrimaryPreferred
,
39
/// In most situations, operations read from among the nearest secondary members, but if no secondaries are
40
/// available, operations read from the primary.
41
kSecondaryPreferred
,
42
/// Operations read from among the nearest members of the replica set, irrespective of the member's type.
43
kNearest
,
44
};
45
46
explicit
ReadPreference(
Mode
mode);
47
ReadPreference(
Mode
mode, std::vector<formats::
bson
::
Document
> tags);
48
49
Mode
GetMode()
const
;
50
std::optional<std::chrono::seconds> GetMaxStaleness()
const
;
51
const
std::vector<formats::
bson
::
Document
>& GetTags()
const
;
52
53
/// @brief Sets maximum replication lag for eligible replica.
54
/// @note Must be at least 90 seconds, cannot be used with kPrimary mode.
55
ReadPreference
&
SetMaxStaleness
(std::optional<std::chrono::seconds> max_staleness);
56
57
/// @brief Adds a tag to the tag set.
58
/// @note Cannot be used with kPrimary mode.
59
ReadPreference
&
AddTag
(formats::
bson
::
Document
tag);
60
61
private
:
62
Mode
mode_;
63
std::optional<std::chrono::seconds> max_staleness_;
64
std::vector<formats::
bson
::
Document
> tags_;
65
};
66
67
/// @brief Read concern
68
/// @see https://docs.mongodb.org/manual/reference/readConcern/
69
enum
class
ReadConcern
{
70
/// no replication checks, default level
71
kLocal
,
72
/// return data replicated to a majority of RS members
73
kMajority
,
74
/// waits for all running majority writes to finish before read
75
kLinearizable
,
76
/// no replication checks, may return orphaned documents if sharded; since 3.6
77
kAvailable
,
78
};
79
80
/// @brief Write concern
81
/// @see https://docs.mongodb.org/manual/reference/write-concern/
82
class
WriteConcern
{
83
public
:
84
enum
Level
{
85
/// Wait until propagation to a "majority" of RS nodes
86
kMajority
,
87
/// Do not check for operation errors, do not wait for write, same as `0`
88
kUnacknowledged
,
89
};
90
91
/// Default timeout for "majority" write concern
92
static
constexpr
std::chrono::seconds
kDefaultMajorityTimeout
{1};
93
94
/// Creates a write concern with the special level
95
explicit
WriteConcern
(
Level
level);
96
97
/// Creates a write concern waiting for propagation to `nodes_count` RS nodes
98
explicit
WriteConcern
(size_t nodes_count);
99
100
/// Creates a write concern defined in RS config
101
explicit
WriteConcern
(std::string tag);
102
103
bool
IsMajority()
const
;
104
size_t NodesCount()
const
;
105
const
std::string& Tag()
const
;
106
std::optional<
bool
> Journal()
const
;
107
const
std::chrono::milliseconds& Timeout()
const
;
108
109
/// Sets write concern timeout, `0` means no timeout
110
WriteConcern
&
SetTimeout
(std::chrono::milliseconds timeout);
111
112
/// Sets whether to wait for on-disk journal commit
113
WriteConcern
&
SetJournal
(
bool
value);
114
115
private
:
116
size_t nodes_count_;
117
bool
is_majority_;
118
std::optional<
bool
> journal_;
119
std::string tag_;
120
std::chrono::milliseconds timeout_;
121
};
122
123
/// Disables ordering on bulk operations causing them to continue after an error
124
class
Unordered
{};
125
126
/// Enables insertion of a new document when update selector matches nothing
127
class
Upsert
{};
128
129
/// Enables automatic one-time retry of duplicate key errors
130
class
RetryDuplicateKey
{};
131
132
/// Specifies that FindAndModify should return the new version of an object
133
class
ReturnNew
{};
134
135
/// Specifies the number of documents to skip
136
class
Skip
{
137
public
:
138
explicit
Skip(size_t value)
139
: value_(value)
140
{}
141
142
size_t Value()
const
{
return
value_; }
143
144
private
:
145
size_t value_;
146
};
147
148
/// @brief Specifies the number of documents to request from the server
149
/// @note The value of `0` means "no limit".
150
class
Limit
{
151
public
:
152
explicit
Limit(size_t value)
153
: value_(value)
154
{}
155
156
size_t Value()
const
{
return
value_; }
157
158
private
:
159
size_t value_;
160
};
161
162
/// @brief Selects fields to be returned
163
/// @note `_id` field is always included by default, order might be significant
164
/// @see
165
/// https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/
166
class
Projection
{
167
public
:
168
/// Creates a default projection including all fields
169
Projection
() =
default
;
170
171
/// Creates a projection including only specified fields
172
Projection
(std::initializer_list<std::string_view> fields_to_include);
173
174
/// Includes a field into the projection
175
Projection
&
Include
(std::string_view field);
176
177
/// @brief Excludes a field from the projection
178
/// @warning Projection cannot have a mix of inclusion and exclusion.
179
/// Only the `_id` field can always be excluded.
180
Projection
&
Exclude
(std::string_view field);
181
182
/// @brief Setups an array slice in the projection
183
/// @param field name of the array field to slice
184
/// @param limit the number of items to return
185
/// @param skip the following number of items
186
/// @note `skip` can be negative, this corresponds to counting from the end
187
/// backwards.
188
/// @note `limit < 0, skip == 0` is equivalent to `limit' = -limit, skip' =
189
/// limit`.
190
/// @warning Cannot be applied to views.
191
Projection
&
Slice
(std::string_view field, int32_t limit, int32_t skip = 0);
192
193
/// @brief Matches the first element of an array satisfying a predicate
194
/// @param field name of the array to search
195
/// @param pred predicate to apply to elements
196
/// @note Array field will be absent from the result if no elements match.
197
/// @note Empty document as a predicate will only match empty documents.
198
Projection
&
ElemMatch
(std::string_view field,
const
formats::
bson
::
Document
& pred);
199
200
/// @cond
201
/// Projection specification BSON access
202
const
bson_t* GetProjectionBson()
const
;
203
/// @endcond
204
205
private
:
206
formats::
bson
::impl::BsonBuilder projection_builder_;
207
};
208
209
/// Sorts the results
210
class
Sort
{
211
public
:
212
enum
Direction {
213
kAscending,
214
kDescending,
215
};
216
217
/// Creates an empty ordering specification
218
Sort
() =
default
;
219
220
/// Stores the specified ordering specification
221
Sort
(std::initializer_list<std::pair<std::string_view, Direction>>);
222
223
/// Appends a field to the ordering specification
224
Sort
&
By
(std::string_view field, Direction direction);
225
226
/// @cond
227
/// Sort specification BSON access
228
const
bson_t* GetSortBson()
const
;
229
/// @endcond
230
231
private
:
232
formats::
bson
::impl::BsonBuilder sort_builder_;
233
};
234
235
/// @brief Specifies an index to use for the query
236
/// @warning Only plans using the index will be considered.
237
class
Hint
{
238
public
:
239
/// Specifies an index by name
240
explicit
Hint
(std::string index_name);
241
242
/// Specifies an index by fields covered
243
explicit
Hint
(formats::
bson
::
Document
index_spec);
244
245
/// @cond
246
/// Retrieves a hint value
247
const
formats::
bson
::
Value
& Value()
const
;
248
/// @endcond
249
250
private
:
251
formats::
bson
::
Value
value_;
252
};
253
254
/// @brief Specifies an array of filter documents that
255
/// determine which array elements to modify for an update
256
/// operation on an array field.
257
class
ArrayFilters
{
258
public
:
259
/// Specifies list of filters
260
explicit
ArrayFilters
(std::initializer_list<formats::
bson
::
Document
>);
261
262
/// Specifies list of filters by container iterators
263
template
<
typename
Iterator>
264
requires
std::is_convertible_v<
typename
std::iterator_traits<Iterator>::value_type, formats::
bson
::
Document
>
265
ArrayFilters
(Iterator first, Iterator last) {
266
formats::
bson
::
ValueBuilder
builder
{
formats
::
common
::
Type
::
kArray
}
;
267
for
(
auto
it = first; it != last; ++it) {
268
builder
.
PushBack
(
*it
)
;
269
}
270
value_ = builder
.
ExtractValue
(
)
;
271
}
272
273
/// @cond
274
/// Retrieves an arrayFilters value
275
const
formats::
bson
::
Value
& Value()
const
;
276
/// @endcond
277
278
private
:
279
formats::
bson
::
Value
value_;
280
};
281
282
/// Suppresses errors on querying a sharded collection with unavailable shards
283
class
AllowPartialResults
{};
284
285
/// @brief Disables exception throw on server errors, should be checked manually
286
/// in WriteResult
287
class
SuppressServerExceptions
{};
288
289
/// @brief Enables tailable cursor, which block at the end of capped collections
290
/// @note Automatically sets `awaitData`.
291
/// @see https://docs.mongodb.com/manual/core/tailable-cursors/
292
class
Tailable
{};
293
294
/// Sets a comment for the operation, which would be visible in profile data
295
class
Comment
{
296
public
:
297
explicit
Comment(std::string);
298
299
const
std::string& Value()
const
;
300
301
private
:
302
std::string value_;
303
};
304
305
/// @brief Specifies the server-side time limit for the operation
306
/// @warning This does not set any client-side timeouts.
307
class
MaxServerTime
{
308
public
:
309
explicit
MaxServerTime(
const
std::chrono::milliseconds& value)
310
: value_(value)
311
{}
312
313
const
std::chrono::milliseconds& Value()
const
{
return
value_; }
314
315
private
:
316
std::chrono::milliseconds value_;
317
};
318
319
/// @brief Specifies collation options for text comparison
320
/// @see https://docs.mongodb.com/manual/reference/collation/
321
/// @see https://unicode-org.github.io/icu/userguide/collation/concepts.html
322
class
Collation
final
{
323
public
:
324
enum
class
Strength
{
325
/// Primary level of comparison (base characters only)
326
kPrimary
= 1,
327
/// Secondary level (base characters + diacritics)
328
kSecondary
= 2,
329
/// Tertiary level (base + diacritics + case), default
330
kTertiary
= 3,
331
/// Quaternary level
332
kQuaternary
= 4,
333
/// Identical level (tie breaker)
334
kIdentical
= 5
335
};
336
337
enum
class
CaseFirst
{
338
/// Default value, similar to lower with slight differences
339
kOff
,
340
/// Uppercase sorts before lowercase
341
kUpper
,
342
/// Lowercase sorts before uppercase
343
kLower
344
};
345
346
enum
class
Alternate
{
347
/// Whitespace and punctuation are considered base characters (default)
348
kNonIgnorable
,
349
/// Whitespace and punctuation not considered base characters
350
kShifted
351
};
352
353
enum
class
MaxVariable
{
354
/// Both whitespace and punctuation are ignorable
355
kPunct
,
356
/// Only whitespace is ignorable
357
kSpace
358
};
359
360
/// Creates a collation with mandatory locale
361
explicit
Collation
(std::string locale);
362
363
/// @brief Sets the ICU collation level
364
/// Default is kTertiary
365
Collation&
SetStrength
(
Strength
strength);
366
367
/// @brief Sets whether to include case comparison at strength level 1 or 2
368
/// Default is false
369
Collation&
SetCaseLevel
(
bool
case_level);
370
371
/// @brief Sets sort order of case differences during tertiary level comparisons
372
/// Default is kOff
373
Collation&
SetCaseFirst
(
CaseFirst
case_first);
374
375
/// @brief Sets whether to compare numeric strings as numbers or as strings
376
/// Default is false (compare as strings)
377
Collation&
SetNumericOrdering
(
bool
numeric_ordering);
378
379
/// @brief Sets whether collation should consider whitespace and punctuation as base characters
380
/// Default is kNonIgnorable
381
Collation&
SetAlternate
(
Alternate
alternate);
382
383
/// @brief Sets up to which characters are considered ignorable when alternate is kShifted
384
/// Has no effect if alternate is kNonIgnorable
385
Collation&
SetMaxVariable
(
MaxVariable
max_variable);
386
387
/// @brief Sets whether strings with diacritics sort from back of the string
388
/// Default is false (compare from front to back)
389
Collation&
SetBackwards
(
bool
backwards);
390
391
/// @brief Sets whether to check if text require normalization and perform normalization
392
/// Default is false
393
Collation&
SetNormalization
(
bool
normalization);
394
395
/// @cond
396
/// Collation specification BSON access for internal use
397
const
bson_t* GetCollationBson()
const
;
398
/// @endcond
399
400
private
:
401
formats::
bson
::impl::BsonBuilder collation_builder_;
402
};
403
404
}
// namespace storages::mongo::options
405
406
USERVER_NAMESPACE_END
userver
storages
mongo
options.hpp
Generated on Mon May 4 2026 12:07:52 for userver by
Doxygen
1.13.2