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

https://github.com/jyemin/mongo-java-driver · Java · 336 lines · 127 code · 31 blank · 178 comment · 1 complexity · 982f09625b2f3a2c1ae1fa3040aa8d51 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.client.model;
  17. import com.mongodb.lang.Nullable;
  18. import org.bson.BsonString;
  19. import org.bson.BsonValue;
  20. import org.bson.conversions.Bson;
  21. import java.util.concurrent.TimeUnit;
  22. import static com.mongodb.assertions.Assertions.notNull;
  23. import static java.util.concurrent.TimeUnit.MILLISECONDS;
  24. /**
  25. * The options to apply to an operation that atomically finds a document and replaces it.
  26. *
  27. * @mongodb.driver.manual reference/command/findAndModify/
  28. * @since 3.0
  29. */
  30. public class FindOneAndReplaceOptions {
  31. private Bson projection;
  32. private Bson sort;
  33. private boolean upsert;
  34. private ReturnDocument returnDocument = ReturnDocument.BEFORE;
  35. private long maxTimeMS;
  36. private Boolean bypassDocumentValidation;
  37. private Collation collation;
  38. private Bson hint;
  39. private String hintString;
  40. private BsonValue comment;
  41. private Bson variables;
  42. /**
  43. * Gets a document describing the fields to return for all matching documents.
  44. *
  45. * @return the project document, which may be null
  46. * @mongodb.driver.manual tutorial/project-fields-from-query-results Projection
  47. */
  48. @Nullable
  49. public Bson getProjection() {
  50. return projection;
  51. }
  52. /**
  53. * Sets a document describing the fields to return for all matching documents.
  54. *
  55. * @param projection the project document, which may be null.
  56. * @return this
  57. * @mongodb.driver.manual tutorial/project-fields-from-query-results Projection
  58. */
  59. public FindOneAndReplaceOptions projection(@Nullable final Bson projection) {
  60. this.projection = projection;
  61. return this;
  62. }
  63. /**
  64. * Gets the sort criteria to apply to the query. The default is null, which means that the documents will be returned in an undefined
  65. * order.
  66. *
  67. * @return a document describing the sort criteria
  68. * @mongodb.driver.manual reference/method/cursor.sort/ Sort
  69. */
  70. @Nullable
  71. public Bson getSort() {
  72. return sort;
  73. }
  74. /**
  75. * Sets the sort criteria to apply to the query.
  76. *
  77. * @param sort the sort criteria, which may be null.
  78. * @return this
  79. * @mongodb.driver.manual reference/method/cursor.sort/ Sort
  80. */
  81. public FindOneAndReplaceOptions sort(@Nullable final Bson sort) {
  82. this.sort = sort;
  83. return this;
  84. }
  85. /**
  86. * Returns true if a new document should be inserted if there are no matches to the query filter. The default is false.
  87. *
  88. * @return true if a new document should be inserted if there are no matches to the query filter
  89. */
  90. public boolean isUpsert() {
  91. return upsert;
  92. }
  93. /**
  94. * Set to true if a new document should be inserted if there are no matches to the query filter.
  95. *
  96. * @param upsert true if a new document should be inserted if there are no matches to the query filter
  97. * @return this
  98. */
  99. public FindOneAndReplaceOptions upsert(final boolean upsert) {
  100. this.upsert = upsert;
  101. return this;
  102. }
  103. /**
  104. * Gets the {@link ReturnDocument} value indicating whether to return the document before it was replaced or after
  105. *
  106. * @return {@link ReturnDocument#BEFORE} if returning the document before it was replaced otherwise return {@link ReturnDocument#AFTER}
  107. */
  108. public ReturnDocument getReturnDocument() {
  109. return returnDocument;
  110. }
  111. /**
  112. * Set whether to return the document before it was replaced or after
  113. *
  114. * @param returnDocument set whether to return the document before it was replaced or after
  115. * @return this
  116. */
  117. public FindOneAndReplaceOptions returnDocument(final ReturnDocument returnDocument) {
  118. this.returnDocument = notNull("returnDocument", returnDocument);
  119. return this;
  120. }
  121. /**
  122. * Sets the maximum execution time on the server for this operation.
  123. *
  124. * @param maxTime the max time
  125. * @param timeUnit the time unit, which may not be null
  126. * @return this
  127. */
  128. public FindOneAndReplaceOptions maxTime(final long maxTime, final TimeUnit timeUnit) {
  129. notNull("timeUnit", timeUnit);
  130. this.maxTimeMS = MILLISECONDS.convert(maxTime, timeUnit);
  131. return this;
  132. }
  133. /**
  134. * Gets the maximum execution time for the find one and replace operation.
  135. *
  136. * @param timeUnit the time unit for the result
  137. * @return the max time
  138. */
  139. public long getMaxTime(final TimeUnit timeUnit) {
  140. return timeUnit.convert(maxTimeMS, MILLISECONDS);
  141. }
  142. /**
  143. * Gets the bypass document level validation flag
  144. *
  145. * @return the bypass document level validation flag
  146. * @since 3.2
  147. * @mongodb.server.release 3.2
  148. */
  149. @Nullable
  150. public Boolean getBypassDocumentValidation() {
  151. return bypassDocumentValidation;
  152. }
  153. /**
  154. * Sets the bypass document level validation flag.
  155. *
  156. * @param bypassDocumentValidation If true, allows the write to opt-out of document level validation.
  157. * @return this
  158. * @since 3.2
  159. * @mongodb.server.release 3.2
  160. */
  161. public FindOneAndReplaceOptions bypassDocumentValidation(@Nullable final Boolean bypassDocumentValidation) {
  162. this.bypassDocumentValidation = bypassDocumentValidation;
  163. return this;
  164. }
  165. /**
  166. * Returns the collation options
  167. *
  168. * @return the collation options
  169. * @since 3.4
  170. * @mongodb.server.release 3.4
  171. */
  172. @Nullable
  173. public Collation getCollation() {
  174. return collation;
  175. }
  176. /**
  177. * Sets the collation options
  178. *
  179. * <p>A null value represents the server default.</p>
  180. * @param collation the collation options to use
  181. * @return this
  182. * @since 3.4
  183. * @mongodb.server.release 3.4
  184. */
  185. public FindOneAndReplaceOptions collation(@Nullable final Collation collation) {
  186. this.collation = collation;
  187. return this;
  188. }
  189. /**
  190. * Returns the hint for which index to use. The default is not to set a hint.
  191. *
  192. * @return the hint
  193. * @since 4.1
  194. */
  195. @Nullable
  196. public Bson getHint() {
  197. return hint;
  198. }
  199. /**
  200. * Sets the hint for which index to use. A null value means no hint is set.
  201. *
  202. * @param hint the hint
  203. * @return this
  204. * @since 4.1
  205. */
  206. public FindOneAndReplaceOptions hint(@Nullable final Bson hint) {
  207. this.hint = hint;
  208. return this;
  209. }
  210. /**
  211. * Gets the hint string to apply.
  212. *
  213. * @return the hint string, which should be the name of an existing index
  214. * @since 4.1
  215. */
  216. @Nullable
  217. public String getHintString() {
  218. return hintString;
  219. }
  220. /**
  221. * Sets the hint to apply.
  222. *
  223. * @param hint the name of the index which should be used for the operation
  224. * @return this
  225. * @since 4.1
  226. */
  227. public FindOneAndReplaceOptions hintString(@Nullable final String hint) {
  228. this.hintString = hint;
  229. return this;
  230. }
  231. /**
  232. * @return the comment for this operation. A null value means no comment is set.
  233. * @since 4.6
  234. * @mongodb.server.release 4.4
  235. */
  236. @Nullable
  237. public BsonValue getComment() {
  238. return comment;
  239. }
  240. /**
  241. * Sets the comment for this operation. A null value means no comment is set.
  242. *
  243. * @param comment the comment
  244. * @return this
  245. * @since 4.6
  246. * @mongodb.server.release 4.4
  247. */
  248. public FindOneAndReplaceOptions comment(@Nullable final String comment) {
  249. this.comment = comment != null ? new BsonString(comment) : null;
  250. return this;
  251. }
  252. /**
  253. * Sets the comment for this operation. A null value means no comment is set.
  254. *
  255. * @param comment the comment
  256. * @return this
  257. * @since 4.6
  258. * @mongodb.server.release 4.4
  259. */
  260. public FindOneAndReplaceOptions comment(@Nullable final BsonValue comment) {
  261. this.comment = comment;
  262. return this;
  263. }
  264. /**
  265. * Add top-level variables to the operation
  266. *
  267. * @return the top level variables if set or null.
  268. * @mongodb.server.release 5.0
  269. * @since 4.6
  270. */
  271. @Nullable
  272. public Bson getLet() {
  273. return variables;
  274. }
  275. /**
  276. * Add top-level variables for the operation
  277. *
  278. * <p>Allows for improved command readability by separating the variables from the query text.
  279. *
  280. * @param variables for the operation or null
  281. * @return this
  282. * @mongodb.server.release 5.0
  283. * @since 4.6
  284. */
  285. public FindOneAndReplaceOptions let(final Bson variables) {
  286. this.variables = variables;
  287. return this;
  288. }
  289. @Override
  290. public String toString() {
  291. return "FindOneAndReplaceOptions{"
  292. + "projection=" + projection
  293. + ", sort=" + sort
  294. + ", upsert=" + upsert
  295. + ", returnDocument=" + returnDocument
  296. + ", maxTimeMS=" + maxTimeMS
  297. + ", bypassDocumentValidation=" + bypassDocumentValidation
  298. + ", collation=" + collation
  299. + ", hint=" + hint
  300. + ", hintString" + hintString
  301. + ", comment=" + comment
  302. + ", let=" + variables
  303. + '}';
  304. }
  305. }