find.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. // Copyright 2014 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. #pragma once
  15. #include <chrono>
  16. #include <cstdint>
  17. #include <bsoncxx/document/view_or_value.hpp>
  18. #include <bsoncxx/stdx/optional.hpp>
  19. #include <bsoncxx/string/view_or_value.hpp>
  20. #include <mongocxx/cursor.hpp>
  21. #include <mongocxx/hint.hpp>
  22. #include <mongocxx/read_preference.hpp>
  23. #include <mongocxx/config/prelude.hpp>
  24. namespace mongocxx {
  25. MONGOCXX_INLINE_NAMESPACE_BEGIN
  26. namespace options {
  27. ///
  28. /// Class representing the optional arguments to a MongoDB query.
  29. ///
  30. class MONGOCXX_API find {
  31. public:
  32. ///
  33. /// Enables writing to temporary files on the server. When set to true, the server
  34. /// can write temporary data to disk while executing the find operation.
  35. ///
  36. /// This option is sent only if the caller explicitly provides a value. The default
  37. /// is to not send a value.
  38. ///
  39. /// This option may only be used with MongoDB version 4.4 or later.
  40. ///
  41. /// @param allow_disk_use
  42. /// Whether to allow writing temporary files on the server.
  43. ///
  44. /// @return
  45. /// A reference to this object to facilitate method chaining.
  46. ///
  47. /// @see https://docs.mongodb.com/manual/reference/command/find/
  48. ///
  49. find& allow_disk_use(bool allow_disk_use);
  50. ///
  51. /// Gets the current setting for allowing disk use on the server.
  52. ///
  53. /// This option may only be used with MongoDB version 4.4 or later.
  54. ///
  55. /// @return Whether disk use on the server is allowed.
  56. ///
  57. const stdx::optional<bool>& allow_disk_use() const;
  58. ///
  59. /// Sets whether to allow partial results from a mongos if some shards are down (instead of
  60. /// throwing an error).
  61. ///
  62. /// @param allow_partial
  63. /// Whether to allow partial results from mongos.
  64. ///
  65. /// @return
  66. /// A reference to the object on which this member function is being called. This facilitates
  67. /// method chaining.
  68. ///
  69. /// @see https://docs.mongodb.com/master/reference/command/find/
  70. ///
  71. find& allow_partial_results(bool allow_partial);
  72. ///
  73. /// Gets the current setting for allowing partial results from mongos.
  74. ///
  75. /// @return Whether partial results from mongos are allowed.
  76. ///
  77. /// @see https://docs.mongodb.com/master/reference/command/find/
  78. ///
  79. const stdx::optional<bool>& allow_partial_results() const;
  80. ///
  81. /// Sets the number of documents to return per batch.
  82. ///
  83. /// @param batch_size
  84. /// The size of the batches to request.
  85. ///
  86. /// @return
  87. /// A reference to the object on which this member function is being called. This facilitates
  88. /// method chaining.
  89. ///
  90. /// @see https://docs.mongodb.com/master/reference/command/find/
  91. ///
  92. find& batch_size(std::int32_t batch_size);
  93. ///
  94. /// The current batch size setting.
  95. ///
  96. /// @return The current batch size.
  97. ///
  98. /// @see https://docs.mongodb.com/master/reference/command/find/
  99. ///
  100. const stdx::optional<std::int32_t>& batch_size() const;
  101. ///
  102. /// Sets the collation for this operation.
  103. ///
  104. /// @param collation
  105. /// The new collation.
  106. ///
  107. /// @return
  108. /// A reference to the object on which this member function is being called. This facilitates
  109. /// method chaining.
  110. ///
  111. /// @see https://docs.mongodb.com/master/reference/command/find/
  112. ///
  113. find& collation(bsoncxx::document::view_or_value collation);
  114. ///
  115. /// Retrieves the current collation for this operation.
  116. ///
  117. /// @return
  118. /// The current collation.
  119. ///
  120. /// @see https://docs.mongodb.com/master/reference/command/find/
  121. ///
  122. const stdx::optional<bsoncxx::document::view_or_value>& collation() const;
  123. ///
  124. /// Attaches a comment to the query. If $comment also exists in the modifiers document then
  125. /// the comment field overwrites $comment.
  126. ///
  127. /// @param comment
  128. /// The comment to attach to this query.
  129. ///
  130. /// @return
  131. /// A reference to the object on which this member function is being called. This facilitates
  132. /// method chaining.
  133. ///
  134. /// @see https://docs.mongodb.com/master/reference/command/find/
  135. ///
  136. find& comment(bsoncxx::string::view_or_value comment);
  137. ///
  138. /// Gets the current comment attached to this query.
  139. ///
  140. /// @return The comment attached to this query.
  141. ///
  142. /// @see https://docs.mongodb.com/master/reference/command/find/
  143. ///
  144. const stdx::optional<bsoncxx::string::view_or_value>& comment() const;
  145. ///
  146. /// Indicates the type of cursor to use for this query.
  147. ///
  148. /// @param cursor_type
  149. /// The cursor type to set.
  150. ///
  151. /// @return
  152. /// A reference to the object on which this member function is being called. This facilitates
  153. /// method chaining.
  154. ///
  155. /// @see https://docs.mongodb.com/master/reference/command/find/
  156. ///
  157. find& cursor_type(cursor::type cursor_type);
  158. ///
  159. /// Gets the current cursor type.
  160. ///
  161. /// @return The current cursor type.
  162. ///
  163. /// @see https://docs.mongodb.com/master/reference/command/find/
  164. ///
  165. const stdx::optional<cursor::type>& cursor_type() const;
  166. ///
  167. /// Sets the index to use for this operation.
  168. ///
  169. /// @see https://docs.mongodb.com/master/reference/command/find/
  170. ///
  171. /// @note if the server already has a cached shape for this query, it may
  172. /// ignore a hint.
  173. ///
  174. /// @param index_hint
  175. /// Object representing the index to use.
  176. ///
  177. /// @return
  178. /// A reference to the object on which this member function is being called. This facilitates
  179. /// method chaining.
  180. ///
  181. find& hint(class hint index_hint);
  182. ///
  183. /// Gets the current hint.
  184. ///
  185. /// @return The current hint, if one is set.
  186. ///
  187. /// @see https://docs.mongodb.com/master/reference/command/find/
  188. ///
  189. const stdx::optional<class hint>& hint() const;
  190. ///
  191. /// Sets maximum number of documents to return.
  192. ///
  193. /// @param limit
  194. /// The maximum number of documents to return.
  195. ///
  196. /// @return
  197. /// A reference to the object on which this member function is being called. This facilitates
  198. /// method chaining.
  199. /// @see https://docs.mongodb.com/master/reference/command/find/
  200. ///
  201. find& limit(std::int64_t limit);
  202. ///
  203. /// Gets the current limit.
  204. ///
  205. /// @return The current limit.
  206. ///
  207. /// @see https://docs.mongodb.com/master/reference/command/find/
  208. ///
  209. const stdx::optional<std::int64_t>& limit() const;
  210. ///
  211. /// Gets the current exclusive upper bound for a specific index.
  212. ///
  213. /// @param max
  214. /// The exclusive upper bound for a specific index.
  215. ///
  216. /// @return
  217. /// A reference to the object on which this member function is being called. This facilitates
  218. /// method chaining.
  219. ///
  220. /// @see https://docs.mongodb.com/master/reference/command/find/
  221. ///
  222. find& (max)(bsoncxx::document::view_or_value max);
  223. ///
  224. /// Sets the current exclusive upper bound for a specific index.
  225. ///
  226. /// @return The exclusive upper bound for a specific index.
  227. ///
  228. /// @see https://docs.mongodb.com/master/reference/command/find/
  229. ///
  230. const stdx::optional<bsoncxx::document::view_or_value>& (max)() const;
  231. ///
  232. /// The maximum amount of time for the server to wait on new documents to satisfy a tailable
  233. /// cursor query. This only applies to a TAILABLE_AWAIT cursor. When the cursor is not a
  234. /// TAILABLE_AWAIT cursor, this option is ignored. The default on the server is to wait for one
  235. /// second.
  236. ///
  237. /// @note On servers < 3.2, this option is ignored.
  238. ///
  239. /// @param max_await_time
  240. /// The max amount of time (in milliseconds) to wait for new documents.
  241. ///
  242. /// @return
  243. /// A reference to the object on which this member function is being called. This facilitates
  244. /// method chaining.
  245. ///
  246. /// @see https://docs.mongodb.com/master/reference/command/find/
  247. ///
  248. find& max_await_time(std::chrono::milliseconds max_await_time);
  249. ///
  250. /// The maximum amount of time for the server to wait on new documents to satisfy a tailable
  251. /// cursor query.
  252. ///
  253. /// @return The current max await time (in milliseconds).
  254. ///
  255. /// @see https://docs.mongodb.com/master/reference/command/find/
  256. ///
  257. const stdx::optional<std::chrono::milliseconds>& max_await_time() const;
  258. ///
  259. /// Sets the maximum amount of time for this operation to run (server-side) in milliseconds.
  260. ///
  261. /// @param max_time
  262. /// The max amount of time (in milliseconds).
  263. ///
  264. /// @return
  265. /// A reference to the object on which this member function is being called. This facilitates
  266. /// method chaining.
  267. ///
  268. /// @see https://docs.mongodb.com/master/reference/command/find/
  269. ///
  270. find& max_time(std::chrono::milliseconds max_time);
  271. ///
  272. /// The current max_time_ms setting.
  273. ///
  274. /// @return The current max time (in milliseconds).
  275. ///
  276. /// @see https://docs.mongodb.com/master/reference/command/find/
  277. ///
  278. const stdx::optional<std::chrono::milliseconds>& max_time() const;
  279. ///
  280. /// Gets the current inclusive lower bound for a specific index.
  281. ///
  282. /// @param min
  283. /// The inclusive lower bound for a specific index.
  284. ///
  285. /// @return
  286. /// A reference to the object on which this member function is being called. This facilitates
  287. /// method chaining.
  288. ///
  289. /// @see https://docs.mongodb.com/master/reference/command/find/
  290. ///
  291. find& (min)(bsoncxx::document::view_or_value min);
  292. ///
  293. /// Sets the current inclusive lower bound for a specific index.
  294. ///
  295. /// @return The inclusive lower bound for a specific index.
  296. ///
  297. /// @see https://docs.mongodb.com/master/reference/command/find/
  298. ///
  299. const stdx::optional<bsoncxx::document::view_or_value>& (min)() const;
  300. ///
  301. /// Sets the cursor flag to prevent cursor from timing out server-side due to a period of
  302. /// inactivity.
  303. ///
  304. /// @param no_cursor_timeout
  305. /// When true prevents the cursor from timing out.
  306. ///
  307. /// @return
  308. /// A reference to the object on which this member function is being called. This facilitates
  309. /// method chaining.
  310. ///
  311. /// @see https://docs.mongodb.com/master/reference/command/find/
  312. ///
  313. find& no_cursor_timeout(bool no_cursor_timeout);
  314. ///
  315. /// Gets the current no_cursor_timeout setting.
  316. ///
  317. /// @return The current no_cursor_timeout setting.
  318. ///
  319. /// @see https://docs.mongodb.com/master/reference/command/find/
  320. ///
  321. const stdx::optional<bool>& no_cursor_timeout() const;
  322. ///
  323. /// Sets a projection which limits the returned fields for all matching documents.
  324. ///
  325. /// @param projection
  326. /// The projection document.
  327. ///
  328. /// @return
  329. /// A reference to the object on which this member function is being called. This facilitates
  330. /// method chaining.
  331. ///
  332. /// @see https://docs.mongodb.com/master/reference/command/find/
  333. ///
  334. find& projection(bsoncxx::document::view_or_value projection);
  335. ///
  336. /// Gets the current projection set on this query.
  337. ///
  338. /// @return The current projection.
  339. ///
  340. /// @see https://docs.mongodb.com/master/reference/command/find/
  341. ///
  342. const stdx::optional<bsoncxx::document::view_or_value>& projection() const;
  343. ///
  344. /// Sets the read_preference for this operation.
  345. ///
  346. /// @param rp
  347. /// The new read_preference.
  348. ///
  349. /// @return
  350. /// A reference to the object on which this member function is being called. This facilitates
  351. /// method chaining.
  352. ///
  353. /// @see https://docs.mongodb.com/master/reference/command/find/
  354. ///
  355. find& read_preference(class read_preference rp);
  356. ///
  357. /// The current read_preference for this operation.
  358. ///
  359. /// @return
  360. /// The current read_preference.
  361. ///
  362. /// @see https://docs.mongodb.com/master/reference/command/find/
  363. ///
  364. const stdx::optional<class read_preference>& read_preference() const;
  365. ///
  366. /// Sets whether to return the index keys associated with the query results, instead of the
  367. /// actual query results themselves.
  368. ///
  369. /// @param return_key
  370. /// Whether to return the index keys associated with the query results, instead of the actual
  371. /// query results themselves.
  372. ///
  373. /// @return
  374. /// A reference to the object on which this member function is being called. This facilitates
  375. /// method chaining.
  376. ///
  377. /// @see https://docs.mongodb.com/master/reference/command/find/
  378. ///
  379. find& return_key(bool return_key);
  380. ///
  381. /// Gets the current setting for returning the index keys associated with the query results,
  382. /// instead of the actual query results themselves.
  383. ///
  384. /// @return
  385. /// Whether index keys associated with the query results are returned, instead of the actual
  386. /// query results themselves.
  387. ///
  388. /// @see https://docs.mongodb.com/master/reference/command/find/
  389. ///
  390. const stdx::optional<bool>& return_key() const;
  391. ///
  392. /// Sets whether to include the record identifier for each document in the query results.
  393. ///
  394. /// @param show_record_id
  395. /// Whether to include the record identifier.
  396. ///
  397. /// @return
  398. /// A reference to the object on which this member function is being called. This facilitates
  399. /// method chaining.
  400. ///
  401. /// @see https://docs.mongodb.com/master/reference/command/find/
  402. ///
  403. find& show_record_id(bool show_record_id);
  404. ///
  405. /// Gets the current setting for whether the record identifier is returned for each document in
  406. /// the query results.
  407. ///
  408. /// @return
  409. /// Whether the record identifier is included.
  410. ///
  411. /// @see https://docs.mongodb.com/master/reference/command/find/
  412. ///
  413. const stdx::optional<bool>& show_record_id() const;
  414. ///
  415. /// Sets the number of documents to skip before returning results.
  416. ///
  417. /// @param skip
  418. /// The number of documents to skip.
  419. ///
  420. /// @return
  421. /// A reference to the object on which this member function is being called. This facilitates
  422. /// method chaining.
  423. ///
  424. /// @see https://docs.mongodb.com/master/reference/command/find/
  425. ///
  426. find& skip(std::int64_t skip);
  427. ///
  428. /// Gets the current number of documents to skip.
  429. ///
  430. /// @return The number of documents to skip.
  431. ///
  432. /// @see https://docs.mongodb.com/master/reference/command/find/
  433. ///
  434. const stdx::optional<std::int64_t>& skip() const;
  435. ///
  436. /// The order in which to return matching documents. If $orderby also exists in the modifiers
  437. /// document, the sort field takes precedence over $orderby.
  438. ///
  439. /// @param ordering
  440. /// Document describing the order of the documents to be returned.
  441. ///
  442. /// @return
  443. /// A reference to the object on which this member function is being called. This facilitates
  444. /// method chaining.
  445. ///
  446. /// @see https://docs.mongodb.com/master/reference/command/find/
  447. ///
  448. find& sort(bsoncxx::document::view_or_value ordering);
  449. ///
  450. /// Gets the current sort ordering for this query.
  451. ///
  452. /// @return The current sort ordering.
  453. ///
  454. /// @see https://docs.mongodb.com/master/reference/command/find/
  455. ///
  456. const stdx::optional<bsoncxx::document::view_or_value>& sort() const;
  457. private:
  458. stdx::optional<bool> _allow_disk_use;
  459. stdx::optional<bool> _allow_partial_results;
  460. stdx::optional<std::int32_t> _batch_size;
  461. stdx::optional<bsoncxx::document::view_or_value> _collation;
  462. stdx::optional<bsoncxx::string::view_or_value> _comment;
  463. stdx::optional<cursor::type> _cursor_type;
  464. stdx::optional<class hint> _hint;
  465. stdx::optional<std::int64_t> _limit;
  466. stdx::optional<bsoncxx::document::view_or_value> _max;
  467. stdx::optional<std::chrono::milliseconds> _max_await_time;
  468. stdx::optional<std::chrono::milliseconds> _max_time;
  469. stdx::optional<bsoncxx::document::view_or_value> _min;
  470. stdx::optional<bool> _no_cursor_timeout;
  471. stdx::optional<bsoncxx::document::view_or_value> _projection;
  472. stdx::optional<class read_preference> _read_preference;
  473. stdx::optional<bool> _return_key;
  474. stdx::optional<bool> _show_record_id;
  475. stdx::optional<std::int64_t> _skip;
  476. stdx::optional<bsoncxx::document::view_or_value> _ordering;
  477. };
  478. } // namespace options
  479. MONGOCXX_INLINE_NAMESPACE_END
  480. } // namespace mongocxx
  481. #include <mongocxx/config/postlude.hpp>