replace_one.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/document/view_or_value.hpp>
  16. #include <bsoncxx/stdx/optional.hpp>
  17. #include <mongocxx/hint.hpp>
  18. #include <mongocxx/stdx.hpp>
  19. #include <mongocxx/config/prelude.hpp>
  20. namespace mongocxx {
  21. MONGOCXX_INLINE_NAMESPACE_BEGIN
  22. namespace model {
  23. ///
  24. /// Class representing a MongoDB update operation that replaces a single document.
  25. ///
  26. class MONGOCXX_API replace_one {
  27. public:
  28. ///
  29. /// Constructs an update operation that will replace a single document matching the filter.
  30. ///
  31. /// @param filter
  32. /// Document representing the criteria for replacement.
  33. /// @param replacement
  34. /// Document that will serve as the replacement.
  35. ///
  36. replace_one(bsoncxx::document::view_or_value filter,
  37. bsoncxx::document::view_or_value replacement);
  38. ///
  39. /// Gets the filter for replacement.
  40. ///
  41. /// @return The filter to be used for the replacement operation.
  42. ///
  43. const bsoncxx::document::view_or_value& filter() const;
  44. ///
  45. /// Gets the replacement document.
  46. ///
  47. /// @return The document that will replace the original selected document.
  48. ///
  49. const bsoncxx::document::view_or_value& replacement() const;
  50. ///
  51. /// Sets the collation for this replacement operation.
  52. ///
  53. /// @param collation
  54. /// The new collation.
  55. ///
  56. /// @see
  57. /// https://docs.mongodb.com/master/reference/collation/
  58. ///
  59. replace_one& collation(bsoncxx::document::view_or_value collation);
  60. ///
  61. /// Gets the collation option for this replacement operation.
  62. ///
  63. /// @return
  64. /// The optional value of the collation option.
  65. ///
  66. /// @see
  67. /// https://docs.mongodb.com/master/reference/collation/
  68. ///
  69. const stdx::optional<bsoncxx::document::view_or_value>& collation() const;
  70. ///
  71. /// Sets the upsert option.
  72. ///
  73. /// When upsert is @c true, this operation will insert the replacement document as a new
  74. /// document if no existing documents match the filter. When upsert is @c false, the
  75. /// replacement operation does nothing if there are no matching documents. By default,
  76. /// upsert is @c false.
  77. ///
  78. /// @param upsert
  79. /// If set to @c true, creates a new document when no document matches the query criteria.
  80. /// The server side default is @c false, which does not insert a new document if a match
  81. /// is not found.
  82. ///
  83. replace_one& upsert(bool upsert);
  84. ///
  85. /// Gets the current value of the upsert option.
  86. ///
  87. /// @return The optional value of the upsert option.
  88. ///
  89. const stdx::optional<bool>& upsert() const;
  90. ///
  91. /// Sets the index to use for this operation.
  92. ///
  93. /// @note if the server already has a cached shape for this query, it may
  94. /// ignore a hint.
  95. ///
  96. /// @param index_hint
  97. /// Object representing the index to use.
  98. ///
  99. /// @return
  100. /// A reference to the object on which this member function is being called. This facilitates
  101. /// method chaining.
  102. ///
  103. replace_one& hint(class hint index_hint);
  104. ///
  105. /// Gets the current hint.
  106. ///
  107. /// @return The current hint, if one is set.
  108. ///
  109. const stdx::optional<class hint>& hint() const;
  110. private:
  111. bsoncxx::document::view_or_value _filter;
  112. bsoncxx::document::view_or_value _replacement;
  113. stdx::optional<bsoncxx::document::view_or_value> _collation;
  114. stdx::optional<bool> _upsert;
  115. stdx::optional<class hint> _hint;
  116. };
  117. } // namespace model
  118. MONGOCXX_INLINE_NAMESPACE_END
  119. } // namespace mongocxx
  120. #include <mongocxx/config/postlude.hpp>