update_many.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 <bsoncxx/array/view_or_value.hpp>
  16. #include <bsoncxx/document/view_or_value.hpp>
  17. #include <bsoncxx/stdx/optional.hpp>
  18. #include <mongocxx/hint.hpp>
  19. #include <mongocxx/stdx.hpp>
  20. #include <mongocxx/config/prelude.hpp>
  21. #include <mongocxx/pipeline.hpp>
  22. namespace mongocxx {
  23. MONGOCXX_INLINE_NAMESPACE_BEGIN
  24. namespace model {
  25. ///
  26. /// Class representing a MongoDB update operation that modifies multiple documents.
  27. ///
  28. class MONGOCXX_API update_many {
  29. //
  30. // Utility class supporting the convenience of {} meaning an empty bsoncxx::document.
  31. //
  32. // Users may not use this class directly.
  33. //
  34. // In places where driver methods take this class as a parameter, passing {} will
  35. // translate to a default-constructed bsoncxx::document::view_or_value,
  36. // regardless of other overloads taking other default-constructible types
  37. // for that parameter. This class avoids compiler ambiguity with such overloads.
  38. //
  39. // See update_many() for an example of such overloads.
  40. //
  41. class _empty_doc_tag {
  42. _empty_doc_tag() = default;
  43. };
  44. public:
  45. ///
  46. /// @{
  47. ///
  48. /// Constructs an update operation that will modify all documents matching the filter.
  49. ///
  50. /// @param filter
  51. /// Document representing the criteria for applying the update.
  52. /// @param update
  53. /// Document representing the modifications to be applied to matching documents.
  54. ///
  55. update_many(bsoncxx::document::view_or_value filter, bsoncxx::document::view_or_value update);
  56. ///
  57. /// Constructs an update operation that will modify all documents matching the filter.
  58. ///
  59. /// @param filter
  60. /// Document representing the criteria for applying the update.
  61. /// @param update
  62. /// Pipeline representing the modifications to be applied to matching documents.
  63. ///
  64. update_many(bsoncxx::document::view_or_value filter, const pipeline& update);
  65. ///
  66. /// Constructs an update operation that will modify all documents matching the filter.
  67. ///
  68. /// @param filter
  69. /// Document representing the criteria for applying the update.
  70. /// @param update
  71. /// Supports the empty update {}.
  72. ///
  73. update_many(bsoncxx::document::view_or_value filter,
  74. std::initializer_list<_empty_doc_tag> update);
  75. ///
  76. /// @}
  77. ///
  78. ///
  79. /// Gets the filter.
  80. ///
  81. /// @return The filter to be used for the update operation.
  82. ///
  83. const bsoncxx::document::view_or_value& filter() const;
  84. ///
  85. /// Gets the update document.
  86. ///
  87. /// @return The modifications to be applied as part of the update.
  88. ///
  89. const bsoncxx::document::view_or_value& update() const;
  90. ///
  91. /// Sets the collation for this update operation.
  92. ///
  93. /// @param collation
  94. /// The new collation.
  95. ///
  96. /// @see
  97. /// https://docs.mongodb.com/master/reference/collation/
  98. ///
  99. update_many& collation(bsoncxx::document::view_or_value collation);
  100. ///
  101. /// Gets the collation option for this update operation.
  102. ///
  103. /// @return
  104. /// The optional value of the collation option.
  105. ///
  106. /// @see
  107. /// https://docs.mongodb.com/master/reference/collation/
  108. ///
  109. const stdx::optional<bsoncxx::document::view_or_value>& collation() const;
  110. /// Sets the index to use for this operation.
  111. ///
  112. /// @note if the server already has a cached shape for this query, it may
  113. /// ignore a hint.
  114. ///
  115. /// @param index_hint
  116. /// Object representing the index to use.
  117. ///
  118. /// @return
  119. /// A reference to the object on which this member function is being called. This facilitates
  120. /// method chaining.
  121. ///
  122. update_many& hint(class hint index_hint);
  123. ///
  124. /// Gets the current hint.
  125. ///
  126. /// @return The current hint, if one is set.
  127. ///
  128. const stdx::optional<class hint>& hint() const;
  129. ///
  130. /// Sets the upsert option.
  131. ///
  132. /// When upsert is @c false, update does nothing when no documents match the filter.
  133. /// However, by specifying upsert as @c true, this operation either updates matching documents
  134. /// or inserts a new document using the update specification if no matching document exists.
  135. /// By default, upsert is unset by the driver, and the server-side default, @c false, is used.
  136. ///
  137. /// @param upsert
  138. /// If set to @c true, creates a new document when no document matches the query criteria.
  139. /// The server side default is @c false, which does not insert a new document if a match
  140. /// is not found.
  141. ///
  142. update_many& upsert(bool upsert);
  143. ///
  144. /// Gets the current value of the upsert option.
  145. ///
  146. /// @return The optional value of the upsert option.
  147. ///
  148. const stdx::optional<bool>& upsert() const;
  149. ///
  150. /// Set array filters for this update operation.
  151. ///
  152. /// @param array_filters
  153. /// Array representing filters determining which array elements to modify.
  154. ///
  155. /// @see https://docs.mongodb.com/manual/reference/command/update/
  156. ///
  157. update_many& array_filters(bsoncxx::array::view_or_value array_filters);
  158. ///
  159. /// Get array filters for this operation.
  160. ///
  161. /// @return
  162. /// The current array filters.
  163. ///
  164. /// @see https://docs.mongodb.com/manual/reference/command/update/
  165. ///
  166. const stdx::optional<bsoncxx::array::view_or_value>& array_filters() const;
  167. private:
  168. bsoncxx::document::view_or_value _filter;
  169. bsoncxx::document::view_or_value _update;
  170. stdx::optional<bsoncxx::document::view_or_value> _collation;
  171. stdx::optional<bsoncxx::array::view_or_value> _array_filters;
  172. stdx::optional<bool> _upsert;
  173. stdx::optional<class hint> _hint;
  174. };
  175. } // namespace model
  176. MONGOCXX_INLINE_NAMESPACE_END
  177. } // namespace mongocxx
  178. #include <mongocxx/config/postlude.hpp>