database.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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 <memory>
  16. #include <string>
  17. #include <bsoncxx/document/view_or_value.hpp>
  18. #include <bsoncxx/string/view_or_value.hpp>
  19. #include <mongocxx/client_session.hpp>
  20. #include <mongocxx/collection.hpp>
  21. #include <mongocxx/gridfs/bucket.hpp>
  22. #include <mongocxx/options/create_collection.hpp>
  23. #include <mongocxx/options/gridfs/bucket.hpp>
  24. #include <mongocxx/read_preference.hpp>
  25. #include <mongocxx/write_concern.hpp>
  26. #include <mongocxx/config/prelude.hpp>
  27. namespace mongocxx {
  28. MONGOCXX_INLINE_NAMESPACE_BEGIN
  29. class client;
  30. ///
  31. /// Class representing a MongoDB database.
  32. ///
  33. /// Acts as a gateway for accessing collections that are contained within a database. It inherits
  34. /// all of its default settings from the client that creates it.
  35. ///
  36. class MONGOCXX_API database {
  37. public:
  38. ///
  39. /// Default constructs a new database. The database is not valid for use and is equivalent
  40. /// to the state of a moved-from database. The only valid actions to take with a default
  41. /// constructed database are to assign to it, or destroy it.
  42. ///
  43. database() noexcept;
  44. ///
  45. /// Move constructs a database.
  46. ///
  47. database(database&&) noexcept;
  48. ///
  49. /// Move assigns a database.
  50. ///
  51. database& operator=(database&&) noexcept;
  52. ///
  53. /// Copy constructs a database.
  54. ///
  55. database(const database&);
  56. ///
  57. /// Copy assigns a database.
  58. ///
  59. database& operator=(const database&);
  60. ///
  61. /// Destroys a database.
  62. ///
  63. ~database();
  64. ///
  65. /// Returns true if the client is valid, meaning it was not default constructed
  66. /// or moved from.
  67. ///
  68. explicit operator bool() const noexcept;
  69. ///
  70. /// @{
  71. ///
  72. /// Runs an aggregation framework pipeline against this database for
  73. /// pipeline stages that do not require an underlying collection,
  74. /// such as $currentOp and $listLocalSessions.
  75. ///
  76. /// @param pipeline
  77. /// The pipeline of aggregation operations to perform.
  78. /// @param options
  79. /// Optional arguments, see mongocxx::options::aggregate.
  80. ///
  81. /// @return A mongocxx::cursor with the results. If the query fails,
  82. /// the cursor throws mongocxx::query_exception when the returned cursor
  83. /// is iterated.
  84. ///
  85. /// @see https://docs.mongodb.com/manual/reference/command/aggregate/#dbcmd.aggregate
  86. ///
  87. /// @note
  88. /// In order to pass a read concern to this, you must use the
  89. /// database level set read concern - database::read_concern(rc).
  90. /// (Write concern supported only for MongoDB 3.4+).
  91. ///
  92. cursor aggregate(const pipeline& pipeline,
  93. const options::aggregate& options = options::aggregate());
  94. ///
  95. /// Runs an aggregation framework pipeline against this database for
  96. /// pipeline stages that do not require an underlying collection,
  97. /// such as $currentOp and $listLocalSessions.
  98. ///
  99. /// @param session
  100. /// The mongocxx::client_session with which to perform the aggregation.
  101. /// @param pipeline
  102. /// The pipeline of aggregation operations to perform.
  103. /// @param options
  104. /// Optional arguments, see mongocxx::options::aggregate.
  105. ///
  106. /// @return A mongocxx::cursor with the results. If the query fails,
  107. /// the cursor throws mongocxx::query_exception when the returned cursor
  108. /// is iterated.
  109. ///
  110. /// @see https://docs.mongodb.com/manual/reference/command/aggregate/#dbcmd.aggregate
  111. ///
  112. /// @note
  113. /// In order to pass a read concern to this, you must use the
  114. /// database level set read concern - database::read_concern(rc).
  115. /// (Write concern supported only for MongoDB 3.4+).
  116. ///
  117. cursor aggregate(const client_session& session,
  118. const pipeline& pipeline,
  119. const options::aggregate& options = options::aggregate());
  120. ///
  121. /// @}
  122. ///
  123. ///
  124. /// @{
  125. ///
  126. /// Runs a command against this database.
  127. ///
  128. /// @see https://docs.mongodb.com/master/reference/method/db.runCommand/
  129. ///
  130. /// @param command document representing the command to be run.
  131. /// @return the result of executing the command.
  132. ///
  133. /// @throws mongocxx::operation_exception if the operation fails.
  134. ///
  135. bsoncxx::document::value run_command(bsoncxx::document::view_or_value command);
  136. ///
  137. /// Runs a command against this database.
  138. ///
  139. /// @see https://docs.mongodb.com/master/reference/method/db.runCommand/
  140. ///
  141. /// @param session The mongocxx::client_session with which to run the command.
  142. /// @param command document representing the command to be run.
  143. /// @return the result of executing the command.
  144. ///
  145. /// @throws mongocxx::operation_exception if the operation fails.
  146. ///
  147. bsoncxx::document::value run_command(const client_session& session,
  148. bsoncxx::document::view_or_value command);
  149. ///
  150. /// Executes a command on a specific server using this database.
  151. ///
  152. /// @see https://docs.mongodb.com/master/reference/method/db.runCommand/
  153. ///
  154. /// @param command document representing the command to be run.
  155. /// @param server_id specifying which server to use.
  156. /// @return the result of executing the command.
  157. ///
  158. /// @throws mongocxx::operation_exception if the operation fails.
  159. ///
  160. bsoncxx::document::value run_command(bsoncxx::document::view_or_value command,
  161. uint32_t server_id);
  162. ///
  163. /// @}
  164. ///
  165. ///
  166. /// @{
  167. ///
  168. /// Explicitly creates a collection in this database with the specified options.
  169. ///
  170. /// @see
  171. /// https://docs.mongodb.com/master/reference/command/create/
  172. ///
  173. /// @param name
  174. /// the new collection's name.
  175. /// @param collection_options
  176. /// the options for the new collection.
  177. /// @param write_concern
  178. /// the write concern to use for this operation. Will default to database
  179. /// set write concern if none passed here.
  180. ///
  181. /// @exception
  182. /// mongocxx::operation_exception if the operation fails.
  183. ///
  184. class collection create_collection(stdx::string_view name,
  185. bsoncxx::document::view_or_value collection_options = {},
  186. const stdx::optional<write_concern>& write_concern = {});
  187. ///
  188. /// Explicitly creates a collection in this database with the specified options.
  189. ///
  190. /// @see
  191. /// https://docs.mongodb.com/master/reference/command/create/
  192. ///
  193. /// @param session
  194. /// The mongocxx::client_session with which to perform the create operation.
  195. /// @param name
  196. /// the new collection's name.
  197. /// @param collection_options
  198. /// the options for the new collection.
  199. /// @param write_concern
  200. /// the write concern to use for this operation. Will default to database
  201. /// set write concern if none passed here.
  202. ///
  203. /// @exception
  204. /// mongocxx::operation_exception if the operation fails.
  205. ///
  206. class collection create_collection(const client_session& session,
  207. stdx::string_view name,
  208. bsoncxx::document::view_or_value collection_options = {},
  209. const stdx::optional<write_concern>& write_concern = {});
  210. ///
  211. /// Explicitly creates a collection in this database with the specified options.
  212. ///
  213. /// @deprecated
  214. /// This overload is deprecated. Call database::create_collection with a
  215. /// bsoncxx::document::view_or_value collection_options instead.
  216. ///
  217. /// @see
  218. /// https://docs.mongodb.com/master/reference/command/create/
  219. ///
  220. /// @param name
  221. /// the new collection's name.
  222. /// @param collection_options
  223. /// the options for the new collection.
  224. /// @param write_concern
  225. /// the write concern to use for this operation. Will default to database
  226. /// set write concern if none passed here.
  227. ///
  228. /// @exception
  229. /// mongocxx::operation_exception if the operation fails.
  230. ///
  231. MONGOCXX_DEPRECATED class collection create_collection(
  232. bsoncxx::string::view_or_value name,
  233. const options::create_collection_deprecated& collection_options,
  234. const stdx::optional<write_concern>& write_concern = {}) {
  235. return create_collection_deprecated(name, collection_options, write_concern);
  236. }
  237. class collection create_collection_deprecated(
  238. bsoncxx::string::view_or_value name,
  239. const options::create_collection_deprecated& collection_options,
  240. const stdx::optional<write_concern>& write_concern = {});
  241. ///
  242. /// Explicitly creates a collection in this database with the specified options.
  243. ///
  244. /// @deprecated
  245. /// This overload is deprecated. Call database::create_collection with a
  246. /// bsoncxx::document::view_or_value collection_options instead.
  247. ///
  248. /// @see
  249. /// https://docs.mongodb.com/master/reference/command/create/
  250. ///
  251. /// @param session
  252. /// The mongocxx::client_session with which to perform the create operation.
  253. /// @param name
  254. /// the new collection's name.
  255. /// @param collection_options
  256. /// the options for the new collection.
  257. /// @param write_concern
  258. /// the write concern to use for this operation. Will default to database
  259. /// set write concern if none passed here.
  260. ///
  261. /// @exception
  262. /// mongocxx::operation_exception if the operation fails.
  263. ///
  264. MONGOCXX_DEPRECATED class collection create_collection(
  265. const client_session& session,
  266. bsoncxx::string::view_or_value name,
  267. const options::create_collection_deprecated& collection_options,
  268. const stdx::optional<write_concern>& write_concern = {}) {
  269. return create_collection_deprecated(session, name, collection_options, write_concern);
  270. }
  271. class collection create_collection_deprecated(
  272. const client_session& session,
  273. bsoncxx::string::view_or_value name,
  274. const options::create_collection_deprecated& collection_options,
  275. const stdx::optional<write_concern>& write_concern = {});
  276. ///
  277. /// @}
  278. ///
  279. ///
  280. /// @{
  281. ///
  282. /// Drops the database and all its collections.
  283. ///
  284. /// @param write_concern (optional)
  285. /// The write concern to be used for this operation. If not passed here, the write concern
  286. /// set on the database will be used.
  287. ///
  288. /// @exception
  289. /// mongocxx::operation_exception if the operation fails.
  290. ///
  291. /// @see
  292. /// https://docs.mongodb.com/manual/reference/command/dropDatabase/
  293. ///
  294. void drop(const bsoncxx::stdx::optional<mongocxx::write_concern>& write_concern = {});
  295. ///
  296. /// Drops the database and all its collections.
  297. ///
  298. /// @param session
  299. /// The mongocxx::client_session with which to perform the aggregation.
  300. /// @param write_concern (optional)
  301. /// The write concern to be used for this operation. If not passed here, the write concern
  302. /// set on the database will be used.
  303. ///
  304. /// @exception
  305. /// mongocxx::operation_exception if the operation fails.
  306. ///
  307. /// @see
  308. /// https://docs.mongodb.com/manual/reference/command/dropDatabase/
  309. ///
  310. void drop(const client_session& session,
  311. const bsoncxx::stdx::optional<mongocxx::write_concern>& write_concern = {});
  312. ///
  313. /// @}
  314. ///
  315. ///
  316. /// Checks whether this database contains a collection having the given name.
  317. ///
  318. /// @param name the name of the collection.
  319. ///
  320. /// @return bool whether the collection exists in this database.
  321. ///
  322. /// @throws mongocxx::operation_exception if the underlying 'listCollections'
  323. /// command fails.
  324. ///
  325. bool has_collection(bsoncxx::string::view_or_value name) const;
  326. ///
  327. /// @{
  328. ///
  329. /// Enumerates the collections in this database.
  330. ///
  331. /// @param filter
  332. /// An optional query expression to filter the returned collections.
  333. ///
  334. /// @return mongocxx::cursor containing the collection information.
  335. ///
  336. /// @see https://docs.mongodb.com/master/reference/command/listCollections/
  337. ///
  338. cursor list_collections(bsoncxx::document::view_or_value filter = {});
  339. ///
  340. /// Enumerates the collections in this database.
  341. ///
  342. /// @param session
  343. /// The mongocxx::client_session with which to perform the aggregation.
  344. /// @param filter
  345. /// An optional query expression to filter the returned collections.
  346. ///
  347. /// @return mongocxx::cursor containing the collection information.
  348. ///
  349. /// @see https://docs.mongodb.com/master/reference/command/listCollections/
  350. ///
  351. cursor list_collections(const client_session& session,
  352. bsoncxx::document::view_or_value filter = {});
  353. ///
  354. /// Enumerates the collection names in this database.
  355. ///
  356. /// @param filter
  357. /// An optional query expression to filter the returned collection names.
  358. ///
  359. /// @return std::vector<std::string> containing the collection names.
  360. ///
  361. /// @throws mongocxx::operation_exception if the underlying 'listCollections'
  362. /// command fails.
  363. ///
  364. /// @see https://docs.mongodb.com/master/reference/command/listCollections/
  365. ///
  366. std::vector<std::string> list_collection_names(bsoncxx::document::view_or_value filter = {});
  367. ///
  368. /// Enumerates the collection names in this database.
  369. ///
  370. /// @param session
  371. /// The mongocxx::client_session with which to perform the aggregation.
  372. /// @param filter
  373. /// An optional query expression to filter the returned collection names.
  374. ///
  375. /// @return std::vector<std::string> containing the collection names.
  376. ///
  377. /// @throws mongocxx::operation_exception if the underlying 'listCollections'
  378. /// command fails.
  379. ///
  380. /// @see https://docs.mongodb.com/master/reference/command/listCollections/
  381. ///
  382. std::vector<std::string> list_collection_names(const client_session& session,
  383. bsoncxx::document::view_or_value filter = {});
  384. ///
  385. /// @}
  386. ///
  387. ///
  388. /// Get the name of this database.
  389. ///
  390. /// @return the name of this database.
  391. ///
  392. stdx::string_view name() const;
  393. ///
  394. /// Sets the read_concern for this database.
  395. ///
  396. /// @note Modifications at this level do not affect existing collection instances that have come
  397. /// from this database, but do affect new ones. New collections will receive a copy of the
  398. /// new read_concern for this database upon instantiation.
  399. ///
  400. /// @param rc
  401. /// The new @c read_concern
  402. ///
  403. /// @see https://docs.mongodb.com/master/reference/read-concern/
  404. ///
  405. void read_concern(class read_concern rc);
  406. ///
  407. /// The current read concern for this database.
  408. ///
  409. /// If the read_concern is not explicitly set on this database object, it inherits the
  410. /// read_concern from its parent client object.
  411. ///
  412. /// @return the current read_concern
  413. ///
  414. class read_concern read_concern() const;
  415. ///
  416. /// Sets the read_preference for this database.
  417. ///
  418. /// @note Modifications at this level do not affect existing collection instances that have come
  419. /// from this database, but do affect new ones. New collections will receive a copy of the
  420. /// new read_preference for this database upon instantiation.
  421. ///
  422. /// @see https://docs.mongodb.com/master/core/read-preference/
  423. ///
  424. /// @param rp the new read_preference.
  425. ///
  426. void read_preference(class read_preference rp);
  427. ///
  428. /// The current read preference for this database.
  429. ///
  430. /// @see https://docs.mongodb.com/master/core/read-preference/
  431. ///
  432. /// @return the current read_preference
  433. ///
  434. class read_preference read_preference() const;
  435. ///
  436. /// Sets the write_concern for this database.
  437. ///
  438. /// @note Modifications at this level do not affect existing collection instances that have come
  439. /// from this database, but do affect new ones as new collections will receive a copy of the
  440. /// write_concern of this database upon instantiation.
  441. ///
  442. void write_concern(class write_concern wc);
  443. ///
  444. /// The current write_concern for this database.
  445. ///
  446. /// @return the current write_concern
  447. ///
  448. class write_concern write_concern() const;
  449. ///
  450. /// Access a collection (logical grouping of documents) within this database.
  451. ///
  452. /// @param name the name of the collection to get.
  453. ///
  454. /// @return the collection.
  455. ///
  456. class collection collection(bsoncxx::string::view_or_value name) const;
  457. ///
  458. /// Allows the db["collection_name"] syntax to be used to access a collection within this
  459. /// database.
  460. ///
  461. /// @param name the name of the collection to get.
  462. ///
  463. /// @return the collection.
  464. ///
  465. MONGOCXX_INLINE class collection operator[](bsoncxx::string::view_or_value name) const;
  466. ///
  467. /// Access a GridFS bucket within this database.
  468. ///
  469. /// @param options
  470. /// The options for the bucket.
  471. ///
  472. /// @return
  473. /// The GridFS bucket.
  474. ///
  475. /// @note
  476. /// See the class comment for `gridfs::bucket` for more information about GridFS.
  477. ///
  478. /// @throws mongocxx::logic_error if `options` are invalid.
  479. ///
  480. class gridfs::bucket gridfs_bucket(
  481. const options::gridfs::bucket& options = options::gridfs::bucket()) const;
  482. ///
  483. /// @{
  484. ///
  485. /// Gets a change stream on this database with an empty pipeline.
  486. /// Change streams are only supported with a "majority" read concern or no read concern.
  487. ///
  488. /// @param options
  489. /// The options to use when creating the change stream.
  490. ///
  491. /// @return
  492. /// A change stream on this database.
  493. ///
  494. /// @see https://docs.mongodb.com/manual/changeStreams/
  495. ///
  496. change_stream watch(const options::change_stream& options = {});
  497. ///
  498. /// @param session
  499. /// The mongocxx::client_session with which to perform the watch operation.
  500. /// @param options
  501. /// The options to use when creating the change stream.
  502. ///
  503. /// @return
  504. /// A change stream on this database.
  505. ///
  506. /// @see https://docs.mongodb.com/manual/changeStreams/
  507. ///
  508. change_stream watch(const client_session& session, const options::change_stream& options = {});
  509. ///
  510. /// Gets a change stream on this database.
  511. /// Change streams are only supported with a "majority" read concern or no read concern.
  512. ///
  513. /// @param pipe
  514. /// The aggregation pipeline to be used on the change notifications.
  515. /// Only a subset of pipeline operations are supported for change streams. For more
  516. /// information see the change streams documentation.
  517. /// @param options
  518. /// The options to use when creating the change stream.
  519. ///
  520. /// @return
  521. /// A change stream on this database.
  522. ///
  523. /// @see https://docs.mongodb.com/manual/changeStreams/
  524. ///
  525. change_stream watch(const pipeline& pipe, const options::change_stream& options = {});
  526. ///
  527. /// Gets a change stream on this database.
  528. ///
  529. /// @param session
  530. /// The mongocxx::client_session with which to perform the watch operation.
  531. /// @param pipe
  532. /// The aggregation pipeline to be used on the change notifications.
  533. /// @param options
  534. /// The options to use when creating the change stream.
  535. ///
  536. /// @return
  537. /// A change stream on this database.
  538. ///
  539. /// @see https://docs.mongodb.com/manual/changeStreams/
  540. ///
  541. change_stream watch(const client_session& session,
  542. const pipeline& pipe,
  543. const options::change_stream& options = {});
  544. ///
  545. /// @}
  546. ///
  547. private:
  548. friend class client;
  549. friend class collection;
  550. MONGOCXX_PRIVATE database(const class client& client, bsoncxx::string::view_or_value name);
  551. MONGOCXX_PRIVATE cursor _aggregate(const client_session* session,
  552. const pipeline& pipeline,
  553. const options::aggregate& options);
  554. MONGOCXX_PRIVATE bsoncxx::document::value _run_command(
  555. const client_session* session, bsoncxx::document::view_or_value command);
  556. MONGOCXX_PRIVATE class collection _create_collection(
  557. const client_session* session,
  558. stdx::string_view name,
  559. bsoncxx::document::view_or_value collection_options,
  560. const stdx::optional<class write_concern>& write_concern);
  561. MONGOCXX_PRIVATE class collection _create_collection_deprecated(
  562. const client_session* session,
  563. bsoncxx::string::view_or_value name,
  564. const options::create_collection_deprecated& collection_options,
  565. const stdx::optional<class write_concern>& write_concern);
  566. MONGOCXX_PRIVATE cursor _list_collections(const client_session* session,
  567. bsoncxx::document::view_or_value filter);
  568. MONGOCXX_PRIVATE std::vector<std::string> _list_collection_names(
  569. const client_session* session, bsoncxx::document::view_or_value filter);
  570. MONGOCXX_PRIVATE void _drop(
  571. const client_session* session,
  572. const bsoncxx::stdx::optional<mongocxx::write_concern>& write_concern);
  573. MONGOCXX_PRIVATE change_stream _watch(const client_session* session,
  574. const pipeline& pipe,
  575. const options::change_stream& options);
  576. class MONGOCXX_PRIVATE impl;
  577. MONGOCXX_PRIVATE impl& _get_impl();
  578. MONGOCXX_PRIVATE const impl& _get_impl() const;
  579. std::unique_ptr<impl> _impl;
  580. };
  581. MONGOCXX_INLINE collection database::operator[](bsoncxx::string::view_or_value name) const {
  582. return collection(name);
  583. }
  584. MONGOCXX_INLINE_NAMESPACE_END
  585. } // namespace mongocxx
  586. #include <mongocxx/config/postlude.hpp>