update_one.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 a single document.
  27. ///
  28. class MONGOCXX_API update_one {
  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_one() 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 a single document 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 the matching document.
  54. ///
  55. update_one(bsoncxx::document::view_or_value filter, bsoncxx::document::view_or_value update);
  56. ///
  57. /// Constructs an update operation that will modify a single document 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 the matching document.
  63. ///
  64. update_one(bsoncxx::document::view_or_value filter, const pipeline& update);
  65. ///
  66. /// Constructs an update operation that will modify a single document 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_one(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_one& 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. ///
  111. /// Sets the index to use for this operation.
  112. ///
  113. /// @note if the server already has a cached shape for this query, it may
  114. /// ignore a hint.
  115. ///
  116. /// @param index_hint
  117. /// Object representing the index to use.
  118. ///
  119. /// @return
  120. /// A reference to the object on which this member function is being called. This facilitates
  121. /// method chaining.
  122. ///
  123. update_one& hint(class hint index_hint);
  124. ///
  125. /// Gets the current hint.
  126. ///
  127. /// @return The current hint, if one is set.
  128. ///
  129. const stdx::optional<class hint>& hint() const;
  130. ///
  131. /// Sets the upsert option.
  132. ///
  133. /// When upsert is @c false, if no document matches the filter, update does nothing.
  134. /// However, by specifying upsert as @c true, this operation either updates a matching document
  135. /// or inserts a new document using the update specification if no matching document exists.
  136. /// By default upsert is @c false.
  137. ///
  138. /// @param upsert
  139. /// If set to @c true, creates a new document when no document matches the query criteria.
  140. /// The server side default is @c false, which does not insert a new document if a match
  141. /// is not found.
  142. ///
  143. update_one& upsert(bool upsert);
  144. ///
  145. /// Gets the current value of the upsert option.
  146. ///
  147. /// @return The optional value of the upsert option.
  148. ///
  149. const stdx::optional<bool>& upsert() const;
  150. ///
  151. /// Set array filters for this update operation.
  152. ///
  153. /// @param array_filters
  154. /// Array representing filters determining which array elements to modify.
  155. ///
  156. /// @see https://docs.mongodb.com/manual/reference/command/update/
  157. ///
  158. update_one& array_filters(bsoncxx::array::view_or_value array_filters);
  159. ///
  160. /// Get array filters for this operation.
  161. ///
  162. /// @return
  163. /// The current array filters.
  164. ///
  165. /// @see https://docs.mongodb.com/manual/reference/command/update/
  166. ///
  167. const stdx::optional<bsoncxx::array::view_or_value>& array_filters() const;
  168. private:
  169. bsoncxx::document::view_or_value _filter;
  170. bsoncxx::document::view_or_value _update;
  171. stdx::optional<bsoncxx::document::view_or_value> _collation;
  172. stdx::optional<bsoncxx::array::view_or_value> _array_filters;
  173. stdx::optional<bool> _upsert;
  174. stdx::optional<class hint> _hint;
  175. };
  176. } // namespace model
  177. MONGOCXX_INLINE_NAMESPACE_END
  178. } // namespace mongocxx
  179. #include <mongocxx/config/postlude.hpp>