/driver-core/src/main/com/mongodb/client/model/FindOneAndUpdateOptions.java

https://github.com/foursquare/mongo-java-driver · Java · 198 lines · 64 code · 19 blank · 115 comment · 0 complexity · 816a155dd443e6b5865d2831543fba61 MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2015 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.client.model;
  17. import org.bson.conversions.Bson;
  18. import java.util.concurrent.TimeUnit;
  19. import static com.mongodb.assertions.Assertions.notNull;
  20. import static java.util.concurrent.TimeUnit.MILLISECONDS;
  21. /**
  22. * The options to apply to an operation that atomically finds a document and updates it.
  23. *
  24. * @since 3.0
  25. * @mongodb.driver.manual reference/command/findAndModify/
  26. */
  27. public class FindOneAndUpdateOptions {
  28. private Bson projection;
  29. private Bson sort;
  30. private boolean upsert;
  31. private ReturnDocument returnDocument = ReturnDocument.BEFORE;
  32. private long maxTimeMS;
  33. private Boolean bypassDocumentValidation;
  34. private Collation collation;
  35. /**
  36. * Gets a document describing the fields to return for all matching documents.
  37. *
  38. * @return the project document, which may be null
  39. * @mongodb.driver.manual tutorial/project-fields-from-query-results Projection
  40. */
  41. public Bson getProjection() {
  42. return projection;
  43. }
  44. /**
  45. * Sets a document describing the fields to return for all matching documents.
  46. *
  47. * @param projection the project document, which may be null.
  48. * @return this
  49. * @mongodb.driver.manual tutorial/project-fields-from-query-results Projection
  50. */
  51. public FindOneAndUpdateOptions projection(final Bson projection) {
  52. this.projection = projection;
  53. return this;
  54. }
  55. /**
  56. * Gets the sort criteria to apply to the query. The default is null, which means that the documents will be returned in an undefined
  57. * order.
  58. *
  59. * @return a document describing the sort criteria
  60. * @mongodb.driver.manual reference/method/cursor.sort/ Sort
  61. */
  62. public Bson getSort() {
  63. return sort;
  64. }
  65. /**
  66. * Sets the sort criteria to apply to the query.
  67. *
  68. * @param sort the sort criteria, which may be null.
  69. * @return this
  70. * @mongodb.driver.manual reference/method/cursor.sort/ Sort
  71. */
  72. public FindOneAndUpdateOptions sort(final Bson sort) {
  73. this.sort = sort;
  74. return this;
  75. }
  76. /**
  77. * Returns true if a new document should be inserted if there are no matches to the query filter. The default is false.
  78. *
  79. * @return true if a new document should be inserted if there are no matches to the query filter
  80. */
  81. public boolean isUpsert() {
  82. return upsert;
  83. }
  84. /**
  85. * Set to true if a new document should be inserted if there are no matches to the query filter.
  86. *
  87. * @param upsert true if a new document should be inserted if there are no matches to the query filter
  88. * @return this
  89. */
  90. public FindOneAndUpdateOptions upsert(final boolean upsert) {
  91. this.upsert = upsert;
  92. return this;
  93. }
  94. /**
  95. * Gets the {@link ReturnDocument} value indicating whether to return the document before it was updated / inserted or after
  96. *
  97. * @return {@link ReturnDocument#BEFORE} if returning the document before it was updated or inserted otherwise
  98. * returns {@link ReturnDocument#AFTER}
  99. */
  100. public ReturnDocument getReturnDocument() {
  101. return returnDocument;
  102. }
  103. /**
  104. * Set whether to return the document before it was updated / inserted or after
  105. *
  106. * @param returnDocument set whether to return the document before it was updated / inserted or after
  107. * @return this
  108. */
  109. public FindOneAndUpdateOptions returnDocument(final ReturnDocument returnDocument) {
  110. this.returnDocument = returnDocument;
  111. return this;
  112. }
  113. /**
  114. * Sets the maximum execution time on the server for this operation.
  115. *
  116. * @param maxTime the max time
  117. * @param timeUnit the time unit, which may not be null
  118. * @return this
  119. */
  120. public FindOneAndUpdateOptions maxTime(final long maxTime, final TimeUnit timeUnit) {
  121. notNull("timeUnit", timeUnit);
  122. this.maxTimeMS = MILLISECONDS.convert(maxTime, timeUnit);
  123. return this;
  124. }
  125. /**
  126. * Gets the maximum execution time for the find one and update operation.
  127. *
  128. * @param timeUnit the time unit for the result
  129. * @return the max time
  130. */
  131. public long getMaxTime(final TimeUnit timeUnit) {
  132. return timeUnit.convert(maxTimeMS, MILLISECONDS);
  133. }
  134. /**
  135. * Gets the the bypass document level validation flag
  136. *
  137. * @return the bypass document level validation flag
  138. * @since 3.2
  139. * @mongodb.server.release 3.2
  140. */
  141. public Boolean getBypassDocumentValidation() {
  142. return bypassDocumentValidation;
  143. }
  144. /**
  145. * Sets the bypass document level validation flag.
  146. *
  147. * @param bypassDocumentValidation If true, allows the write to opt-out of document level validation.
  148. * @return this
  149. * @since 3.2
  150. * @mongodb.server.release 3.2
  151. */
  152. public FindOneAndUpdateOptions bypassDocumentValidation(final Boolean bypassDocumentValidation) {
  153. this.bypassDocumentValidation = bypassDocumentValidation;
  154. return this;
  155. }
  156. /**
  157. * Returns the collation options
  158. *
  159. * @return the collation options
  160. * @since 3.4
  161. * @mongodb.server.release 3.4
  162. */
  163. public Collation getCollation() {
  164. return collation;
  165. }
  166. /**
  167. * Sets the collation options
  168. *
  169. * <p>A null value represents the server default.</p>
  170. * @param collation the collation options to use
  171. * @return this
  172. * @since 3.4
  173. * @mongodb.server.release 3.4
  174. */
  175. public FindOneAndUpdateOptions collation(final Collation collation) {
  176. this.collation = collation;
  177. return this;
  178. }
  179. }