bulk_write.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2014-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. #pragma once
  15. #include <mongocxx/client_session.hpp>
  16. #include <mongocxx/model/write.hpp>
  17. #include <mongocxx/options/bulk_write.hpp>
  18. #include <mongocxx/result/bulk_write.hpp>
  19. #include <mongocxx/config/prelude.hpp>
  20. namespace mongocxx {
  21. MONGOCXX_INLINE_NAMESPACE_BEGIN
  22. class collection;
  23. ///
  24. /// Class representing a batch of write operations that can be sent to the server as a group.
  25. ///
  26. /// If you have a lot of write operations to execute, it can be more efficient to send them as
  27. /// part of a bulk_write in order to avoid unnecessary network-level round trips between the driver
  28. /// and the server.
  29. ///
  30. /// Bulk writes affect a single collection only and are executed via the bulk_write::execute()
  31. /// method. Options that you would typically specify for individual write operations (such as write
  32. /// concern) are instead specified for the aggregate operation.
  33. ///
  34. /// @see https://docs.mongodb.com/master/core/crud/
  35. /// @see https://docs.mongodb.com/master/core/bulk-write-operations/
  36. ///
  37. class MONGOCXX_API bulk_write {
  38. public:
  39. ///
  40. /// Move constructs a bulk write operation.
  41. ///
  42. bulk_write(bulk_write&&) noexcept;
  43. ///
  44. /// Move assigns a bulk write operation.
  45. ///
  46. bulk_write& operator=(bulk_write&&) noexcept;
  47. ///
  48. /// Destroys a bulk write operation.
  49. ///
  50. ~bulk_write();
  51. ///
  52. /// Appends a single write to the bulk write operation. The write operation's contents are
  53. /// copied into the bulk operation completely, so there is no dependency between the life of an
  54. /// appended write operation and the bulk operation itself.
  55. ///
  56. /// @param operation
  57. /// The write operation to append (an instance of model::write)
  58. ///
  59. /// A model::write can be implicitly constructed from any of the following MongoDB models:
  60. ///
  61. /// - model::insert_one
  62. /// - model::delete_one
  63. /// - model::replace_one
  64. /// - model::update_many
  65. /// - model::update_one
  66. ///
  67. /// @return
  68. /// A reference to the object on which this member function is being called. This facilitates
  69. /// method chaining.
  70. ///
  71. /// @throws mongocxx::logic_error if the given operation is invalid.
  72. ///
  73. bulk_write& append(const model::write& operation);
  74. ///
  75. /// Executes a bulk write.
  76. ///
  77. /// @throws mongocxx::bulk_write_exception when there are errors processing the writes.
  78. ///
  79. /// @return The optional result of the bulk operation execution, a result::bulk_write.
  80. ///
  81. /// @see https://docs.mongodb.com/master/core/bulk-write-operations/
  82. ///
  83. stdx::optional<result::bulk_write> execute() const;
  84. private:
  85. friend class collection;
  86. class MONGOCXX_PRIVATE impl;
  87. MONGOCXX_PRIVATE bulk_write(const collection& coll,
  88. const options::bulk_write& options,
  89. const client_session* session = nullptr);
  90. bool _created_from_collection;
  91. std::unique_ptr<impl> _impl;
  92. };
  93. MONGOCXX_INLINE_NAMESPACE_END
  94. } // namespace mongocxx
  95. #include <mongocxx/config/postlude.hpp>