/driver-core/src/main/com/mongodb/bulk/UpdateRequest.java

https://github.com/mebigfatguy/mongo-java-driver · Java · 170 lines · 64 code · 19 blank · 87 comment · 8 complexity · 2c63655e56003f461c924dbfa4116b8d MD5 · raw file

  1. /*
  2. * Copyright 2008-present MongoDB, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.mongodb.bulk;
  17. import com.mongodb.client.model.Collation;
  18. import org.bson.BsonDocument;
  19. import java.util.List;
  20. import static com.mongodb.assertions.Assertions.notNull;
  21. /**
  22. * An update to one or more documents.
  23. *
  24. * @since 3.0
  25. */
  26. public final class UpdateRequest extends WriteRequest {
  27. private final BsonDocument update;
  28. private final Type updateType;
  29. private final BsonDocument filter;
  30. private boolean isMulti = true;
  31. private boolean isUpsert = false;
  32. private Collation collation;
  33. private List<BsonDocument> arrayFilters;
  34. /**
  35. * Construct a new instance.
  36. * @param filter the non-null query filter
  37. * @param update the non-null update operations
  38. * @param updateType the update type, which must be either UPDATE or REPLACE
  39. */
  40. public UpdateRequest(final BsonDocument filter, final BsonDocument update, final Type updateType) {
  41. if (updateType != Type.UPDATE && updateType != Type.REPLACE) {
  42. throw new IllegalArgumentException("Update type must be UPDATE or REPLACE");
  43. }
  44. this.filter = notNull("filter", filter);
  45. this.update = notNull("update", update);
  46. this.updateType = updateType;
  47. this.isMulti = updateType == Type.UPDATE;
  48. }
  49. @Override
  50. public Type getType() {
  51. return updateType;
  52. }
  53. /**
  54. * Gets the query filter for the update.
  55. *
  56. * @return the filter
  57. */
  58. public BsonDocument getFilter() {
  59. return filter;
  60. }
  61. /**
  62. * Gets the update.
  63. *
  64. * @return the update
  65. */
  66. public BsonDocument getUpdate() {
  67. return update;
  68. }
  69. /**
  70. * Gets whether this update will update all documents matching the filter. The default is true.
  71. *
  72. * @return whether this update will update all documents matching the filter
  73. */
  74. public boolean isMulti() {
  75. return isMulti;
  76. }
  77. /**
  78. * Sets whether this will update all documents matching the query filter.
  79. *
  80. * @param isMulti whether this will update all documents matching the query filter
  81. * @return this
  82. */
  83. public UpdateRequest multi(final boolean isMulti) {
  84. if (isMulti && updateType == Type.REPLACE) {
  85. throw new IllegalArgumentException("Replacements can not be multi");
  86. }
  87. this.isMulti = isMulti;
  88. return this;
  89. }
  90. /**
  91. * Gets whether this update will insert a new document if no documents match the filter. The default is false.
  92. * @return whether this update will insert a new document if no documents match the filter
  93. */
  94. public boolean isUpsert() {
  95. return isUpsert;
  96. }
  97. /**
  98. * Sets whether this update will insert a new document if no documents match the filter.
  99. * @param isUpsert whether this update will insert a new document if no documents match the filter
  100. * @return this
  101. */
  102. public UpdateRequest upsert(final boolean isUpsert) {
  103. this.isUpsert = isUpsert;
  104. return this;
  105. }
  106. /**
  107. * Returns the collation options
  108. *
  109. * @return the collation options
  110. * @since 3.4
  111. * @mongodb.server.release 3.4
  112. */
  113. public Collation getCollation() {
  114. return collation;
  115. }
  116. /**
  117. * Sets the collation options
  118. *
  119. * <p>A null value represents the server default.</p>
  120. * @param collation the collation options to use
  121. * @return this
  122. * @since 3.4
  123. * @mongodb.server.release 3.4
  124. */
  125. public UpdateRequest collation(final Collation collation) {
  126. this.collation = collation;
  127. return this;
  128. }
  129. /**
  130. * Sets the array filters option
  131. *
  132. * @param arrayFilters the array filters, which may be null
  133. * @return this
  134. * @since 3.6
  135. * @mongodb.server.release 3.6
  136. */
  137. public UpdateRequest arrayFilters(final List<BsonDocument> arrayFilters) {
  138. this.arrayFilters = arrayFilters;
  139. return this;
  140. }
  141. /**
  142. * Returns the array filters option
  143. *
  144. * @return the array filters, which may be null
  145. * @since 3.6
  146. * @mongodb.server.release 3.6
  147. */
  148. public List<BsonDocument> getArrayFilters() {
  149. return arrayFilters;
  150. }
  151. }