MongoDB C++ Driver 4.2.0
Loading...
Searching...
No Matches
find.hpp
Go to the documentation of this file.
1// Copyright 2009-present MongoDB, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <mongocxx/options/find-fwd.hpp> // IWYU pragma: export
18
19//
20
22
24#include <mongocxx/v1/find_options.hpp> // IWYU pragma: export
25
26#include <chrono>
27#include <cstdint>
28#include <utility>
29
37
38#include <mongocxx/cursor.hpp>
39#include <mongocxx/hint.hpp>
41
43
44namespace mongocxx {
45namespace v_noabi {
46namespace options {
47
51class find {
52 public:
56 find() = default;
57
61 /* explicit(false) */ MONGOCXX_ABI_EXPORT_CDECL() find(v1::find_options opts);
62
69 explicit operator v1::find_options() const {
72
74
75 if (_allow_disk_use) {
76 ret.allow_disk_use(*_allow_disk_use);
77 }
78
79 if (_allow_partial_results) {
80 ret.allow_partial_results(*_allow_partial_results);
81 }
82
83 if (_batch_size) {
84 ret.batch_size(*_batch_size);
85 }
86
87 if (_collation) {
88 ret.collation(bsoncxx::v1::document::value{to_v1(_collation->view())});
89 }
90
91 if (_cursor_type) {
92 ret.cursor_type(static_cast<v1::cursor::type>(*_cursor_type));
93 }
94
95 if (_hint) {
96 ret.hint(to_v1(*_hint));
97 }
98
99 if (_let) {
100 ret.let(bsoncxx::v1::document::value{to_v1(_let->view())});
101 }
102
103 if (_comment_option) {
104 ret.comment(bsoncxx::v1::types::value{to_v1(_comment_option->view())});
105 } else if (_comment) {
106 ret.comment(bsoncxx::v1::types::value{to_v1(_comment->view())});
107 }
108
109 if (_limit) {
110 ret.limit(*_limit);
111 }
112
113 if (_max) {
114 ret.max(bsoncxx::v1::document::value{to_v1(_max->view())});
115 }
116
117 if (_max_await_time) {
118 ret.max_await_time(*_max_await_time);
119 }
120
121 if (_max_time) {
122 ret.max_time(*_max_time);
123 }
124
125 if (_min) {
126 ret.min(bsoncxx::v1::document::value{to_v1(_min->view())});
127 }
128
129 if (_no_cursor_timeout) {
130 ret.no_cursor_timeout(*_no_cursor_timeout);
131 }
132
133 if (_projection) {
134 ret.projection(bsoncxx::v1::document::value{to_v1(_projection->view())});
135 }
136
137 if (_read_preference) {
138 ret.read_preference(to_v1(*_read_preference));
139 }
140
141 if (_return_key) {
142 ret.return_key(*_return_key);
143 }
144
145 if (_show_record_id) {
146 ret.show_record_id(*_show_record_id);
147 }
148
149 if (_skip) {
150 ret.skip(*_skip);
151 }
152
153 if (_ordering) {
154 ret.sort(bsoncxx::v1::document::value{to_v1(_ordering->view())});
155 }
156
157 return ret;
158 }
159
179 _allow_disk_use = allow_disk_use;
180 return *this;
181 }
182
191 return _allow_disk_use;
192 }
193
208 find& allow_partial_results(bool allow_partial) {
209 _allow_partial_results = allow_partial;
210 return *this;
211 }
212
222 return _allow_partial_results;
223 }
224
238 find& batch_size(std::int32_t batch_size) {
239 _batch_size = batch_size;
240 return *this;
241 }
242
252 return _batch_size;
253 }
254
269 _collation = std::move(collation);
270 return *this;
271 }
272
285
303 _comment = std::move(comment);
304 return *this;
305 }
306
320
334 find& cursor_type(cursor::type cursor_type) {
335 _cursor_type = cursor_type;
336 return *this;
337 }
338
348 return _cursor_type;
349 }
350
367 find& hint(v_noabi::hint index_hint) {
368 _hint = std::move(index_hint);
369 return *this;
370 }
371
381 return _hint;
382 }
383
398 _let = std::move(let);
399 return *this;
400 }
401
414
431 _comment_option = std::move(comment);
432 return *this;
433 }
434
449
462 find& limit(std::int64_t limit) {
463 _limit = limit;
464 return *this;
465 }
466
476 return _limit;
477 }
478
493 _max = std::move(max);
494 return *this;
495 }
496
508
527 find& max_await_time(std::chrono::milliseconds max_await_time) {
528 _max_await_time = max_await_time;
529 return *this;
530 }
531
542 return _max_await_time;
543 }
544
558 find& max_time(std::chrono::milliseconds max_time) {
559 _max_time = max_time;
560 return *this;
561 }
562
572 return _max_time;
573 }
574
589 _min = std::move(min);
590 return *this;
591 }
592
604
620 _no_cursor_timeout = no_cursor_timeout;
621 return *this;
622 }
623
633 return _no_cursor_timeout;
634 }
635
650 _projection = std::move(projection);
651 return *this;
652 }
653
665
680 _read_preference = std::move(rp);
681 return *this;
682 }
683
694 return _read_preference;
695 }
696
713 _return_key = return_key;
714 return *this;
715 }
716
729 return _return_key;
730 }
731
746 _show_record_id = show_record_id;
747 return *this;
748 }
749
761 return _show_record_id;
762 }
763
777 find& skip(std::int64_t skip) {
778 _skip = skip;
779 return *this;
780 }
781
791 return _skip;
792 }
793
809 _ordering = std::move(ordering);
810 return *this;
811 }
812
824
825 private:
827 bsoncxx::v_noabi::stdx::optional<bool> _allow_partial_results;
847};
848
849} // namespace options
850} // namespace v_noabi
851} // namespace mongocxx
852
853namespace mongocxx {
854namespace v_noabi {
855
860 return {std::move(v)};
861}
862
867 return v1::find_options{v};
868}
869
870} // namespace v_noabi
871} // namespace mongocxx
872
874
A BSON document.
Definition value.hpp:46
A polyfill for std::optional<T>.
Definition optional.hpp:799
A union of BSON type values.
Definition value.hpp:54
A view-or-value variant type for strings.
Definition view_or_value.hpp:41
find()=default
Default initialization.
type
Enumeration identifying the type of the cursor.
Definition cursor.hpp:50
Options for a "find" command.
Definition find_options.hpp:72
find_options & collation(bsoncxx::v1::document::value collation)
Set the "collation" field.
find_options & read_preference(v1::read_preference rp)
Set the "readPreference" field.
find_options & allow_partial_results(bool allow_partial)
Set the "allowPartialResults" field.
find_options & return_key(bool return_key)
Set the "returnKey" field.
find_options & batch_size(std::int32_t batch_size)
Set the "batchSize" field.
find_options & skip(std::int64_t skip)
Set the "skip" field.
find_options & comment(bsoncxx::v1::types::value comment)
Set the "comment" field.
find_options & min(bsoncxx::v1::document::value min)
Set the "min" field.
find_options & no_cursor_timeout(bool no_cursor_timeout)
Set the "noCursorTimeout" field.
find_options & allow_disk_use(bool allow_disk_use)
Set the "allowDiskUse" field.
find_options & show_record_id(bool show_record_id)
Set the "showRecordId" field.
find_options & cursor_type(v1::cursor::type cursor_type)
Set the "cursorType" field.
find_options & sort(bsoncxx::v1::document::value ordering)
Set the "sort" field.
find_options & limit(std::int64_t limit)
Set the "limit" field.
find_options & let(bsoncxx::v1::document::value let)
Set the "let" field.
find_options & max_await_time(std::chrono::milliseconds max_await_time)
Set the "maxAwaitTimeMS" field.
find_options & max_time(std::chrono::milliseconds max_time)
Set the "maxTimeMS" field.
find_options & max(bsoncxx::v1::document::value max)
Set the "max" field.
find_options & projection(bsoncxx::v1::document::value projection)
Set the "projection" field.
find_options & hint(v1::hint index_hint)
Set the "hint" field.
The index to "hint" or force a MongoDB server to use when performing a query.
Definition hint.hpp:42
Used by MongoDB find operations.
Definition find.hpp:51
bsoncxx::v_noabi::stdx::optional< bsoncxx::v_noabi::document::view_or_value > const & collation() const
Retrieves the current collation for this operation.
Definition find.hpp:282
bsoncxx::v_noabi::stdx::optional< cursor::type > const & cursor_type() const
Gets the current cursor type.
Definition find.hpp:347
bsoncxx::v_noabi::stdx::optional< bsoncxx::v_noabi::document::view_or_value > const & min() const
Sets the current inclusive lower bound for a specific index.
Definition find.hpp:601
find & sort(bsoncxx::v_noabi::document::view_or_value ordering)
The order in which to return matching documents. If $orderby also exists in the modifiers document,...
Definition find.hpp:808
find & let(bsoncxx::v_noabi::document::view_or_value let)
Set the value of the let option.
Definition find.hpp:397
find & max_await_time(std::chrono::milliseconds max_await_time)
The maximum amount of time for the server to wait on new documents to satisfy a tailable cursor query...
Definition find.hpp:527
bsoncxx::v_noabi::stdx::optional< v_noabi::hint > const & hint() const
Gets the current hint.
Definition find.hpp:380
find & return_key(bool return_key)
Sets whether to return the index keys associated with the query results, instead of the actual query ...
Definition find.hpp:712
find & comment_option(bsoncxx::v_noabi::types::bson_value::view_or_value comment)
Set the value of the comment option.
Definition find.hpp:430
bsoncxx::v_noabi::stdx::optional< bsoncxx::v_noabi::document::view_or_value > const & sort() const
Gets the current sort ordering for this query.
Definition find.hpp:821
find & no_cursor_timeout(bool no_cursor_timeout)
Sets the cursor flag to prevent cursor from timing out server-side due to a period of inactivity.
Definition find.hpp:619
find & hint(v_noabi::hint index_hint)
Sets the index to use for this operation.
Definition find.hpp:367
find & allow_partial_results(bool allow_partial)
Sets whether to allow partial results from a mongos if some shards are down (instead of throwing an e...
Definition find.hpp:208
bsoncxx::v_noabi::stdx::optional< bool > const & allow_partial_results() const
Gets the current setting for allowing partial results from mongos.
Definition find.hpp:221
find()=default
Default initialization.
bsoncxx::v_noabi::stdx::optional< std::int64_t > const & limit() const
Gets the current limit.
Definition find.hpp:475
find & allow_disk_use(bool allow_disk_use)
Enables writing to temporary files on the server. When set to true, the server can write temporary da...
Definition find.hpp:178
find & limit(std::int64_t limit)
Sets maximum number of documents to return.
Definition find.hpp:462
bsoncxx::v_noabi::stdx::optional< bsoncxx::v_noabi::document::view_or_value > const let() const
Gets the current value of the let option.
Definition find.hpp:411
bsoncxx::v_noabi::stdx::optional< bool > const & show_record_id() const
Gets the current setting for whether the record identifier is returned for each document in the query...
Definition find.hpp:760
find & max_time(std::chrono::milliseconds max_time)
Sets the maximum amount of time for this operation to run (server-side) in milliseconds.
Definition find.hpp:558
bsoncxx::v_noabi::stdx::optional< std::int32_t > const & batch_size() const
The current batch size setting.
Definition find.hpp:251
find & cursor_type(cursor::type cursor_type)
Indicates the type of cursor to use for this query.
Definition find.hpp:334
find & show_record_id(bool show_record_id)
Sets whether to include the record identifier for each document in the query results.
Definition find.hpp:745
find & min(bsoncxx::v_noabi::document::view_or_value min)
Gets the current inclusive lower bound for a specific index.
Definition find.hpp:588
bsoncxx::v_noabi::stdx::optional< bool > const & allow_disk_use() const
Gets the current setting for allowing disk use on the server.
Definition find.hpp:190
bsoncxx::v_noabi::stdx::optional< bool > const & no_cursor_timeout() const
Gets the current no_cursor_timeout setting.
Definition find.hpp:632
operator v1::find_options() const
Convert to the mongocxx::v1 equivalent.
Definition find.hpp:69
find & skip(std::int64_t skip)
Sets the number of documents to skip before returning results.
Definition find.hpp:777
bsoncxx::v_noabi::stdx::optional< bsoncxx::v_noabi::types::bson_value::view_or_value > const & comment_option() const
Gets the current value of the comment option.
Definition find.hpp:446
find & comment(bsoncxx::v_noabi::string::view_or_value comment)
Attaches a comment to the query. If $comment also exists in the modifiers document then the comment f...
Definition find.hpp:302
bsoncxx::v_noabi::stdx::optional< std::int64_t > const & skip() const
Gets the current number of documents to skip.
Definition find.hpp:790
find & read_preference(v_noabi::read_preference rp)
Sets the read_preference for this operation.
Definition find.hpp:679
find & projection(bsoncxx::v_noabi::document::view_or_value projection)
Sets a projection which limits the returned fields for all matching documents.
Definition find.hpp:649
bsoncxx::v_noabi::stdx::optional< std::chrono::milliseconds > const & max_await_time() const
The maximum amount of time for the server to wait on new documents to satisfy a tailable cursor query...
Definition find.hpp:541
bsoncxx::v_noabi::stdx::optional< bsoncxx::v_noabi::document::view_or_value > const & max() const
Sets the current exclusive upper bound for a specific index.
Definition find.hpp:505
bsoncxx::v_noabi::stdx::optional< bsoncxx::v_noabi::string::view_or_value > const & comment() const
Gets the current comment attached to this query.
Definition find.hpp:317
find & max(bsoncxx::v_noabi::document::view_or_value max)
Gets the current exclusive upper bound for a specific index.
Definition find.hpp:492
bsoncxx::v_noabi::stdx::optional< std::chrono::milliseconds > const & max_time() const
The current max_time_ms setting.
Definition find.hpp:571
bsoncxx::v_noabi::stdx::optional< bsoncxx::v_noabi::document::view_or_value > const & projection() const
Gets the current projection set on this query.
Definition find.hpp:662
find & batch_size(std::int32_t batch_size)
Sets the number of documents to return per batch.
Definition find.hpp:238
bsoncxx::v_noabi::stdx::optional< v_noabi::read_preference > const & read_preference() const
The current read_preference for this operation.
Definition find.hpp:693
bsoncxx::v_noabi::stdx::optional< bool > const & return_key() const
Gets the current setting for returning the index keys associated with the query results,...
Definition find.hpp:728
find & collation(bsoncxx::v_noabi::document::view_or_value collation)
Sets the collation for this operation.
Definition find.hpp:268
Describes how MongoDB clients route read operations to the members of a replica set or sharded cluste...
Definition read_preference.hpp:59
Provides bsoncxx::v_noabi::document::view_or_value.
Declares mongocxx::v_noabi::options::find.
Provides mongocxx::v1::find_options.
#define MONGOCXX_ABI_EXPORT_CDECL(...)
Equivalent to MONGOCXX_ABI_EXPORT with MONGOCXX_ABI_CDECL.
Definition export.hpp:52
The mongocxx v_noabi macro guard postlude header.
The mongocxx v_noabi macro guard prelude header.
v_noabi::view_or_value< v_noabi::document::view, v_noabi::document::value > view_or_value
Equivalent to v_noabi::view_or_value<v_noabi::document::view, v_noabi::document::value>.
Definition view_or_value.hpp:30
v_noabi::view_or_value< view, value > view_or_value
Equivalent to v_noabi::view_or_value<view, value>.
Definition view_or_value.hpp:31
v1::element::view to_v1(v_noabi::array::element const &v)
Convert to the bsoncxx::v1 equivalent of v.
Definition element.hpp:132
Declares entities whose ABI stability is guaranteed for documented symbols.
Declares entities representing options to use with various commands.
Declares entities whose ABI stability is NOT guaranteed.
v1::bulk_write to_v1(v_noabi::bulk_write v)
Convert to the mongocxx::v1 equivalent of v.
Definition bulk_write.hpp:162
v_noabi::bulk_write from_v1(v1::bulk_write v)
Convert to the mongocxx::v_noabi equivalent of v.
Definition bulk_write.hpp:155
The top-level namespace within which all mongocxx library entities are declared.
Provides bsoncxx::v_noabi::string::view_or_value.
Provides bsoncxx::v_noabi::types::bson_value::view_or_value.
Provides mongocxx::v1::cursor.
Provides bsoncxx::v1::document::value.
Provides bsoncxx::v_noabi::document::view.
Provides std::optional-related polyfills for library API usage.
Provides bsoncxx::v_noabi::types::bson_value::value.
Provides bsoncxx::v_noabi::types::bson_value::view.
Provides mongocxx::v_noabi::cursor.
Provides mongocxx::v_noabi::hint.
Provides mongocxx::v_noabi::read_preference.