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

https://github.com/jyemin/mongo-java-driver · Java · 270 lines · 99 code · 24 blank · 147 comment · 1 complexity · 57811ac96a562bf55bb3c51cd10c64b9 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 deletes it.
  26. *
  27. * @since 3.0
  28. * @mongodb.driver.manual reference/command/findAndModify/
  29. */
  30. public class FindOneAndDeleteOptions {
  31. private Bson projection;
  32. private Bson sort;
  33. private long maxTimeMS;
  34. private Collation collation;
  35. private Bson hint;
  36. private String hintString;
  37. private BsonValue comment;
  38. private Bson variables;
  39. /**
  40. * Gets a document describing the fields to return for all matching documents.
  41. *
  42. * @return the project document, which may be null
  43. * @mongodb.driver.manual tutorial/project-fields-from-query-results Projection
  44. */
  45. @Nullable
  46. public Bson getProjection() {
  47. return projection;
  48. }
  49. /**
  50. * Sets a document describing the fields to return for all matching documents.
  51. *
  52. * @param projection the project document, which may be null.
  53. * @return this
  54. * @mongodb.driver.manual tutorial/project-fields-from-query-results Projection
  55. */
  56. public FindOneAndDeleteOptions projection(@Nullable final Bson projection) {
  57. this.projection = projection;
  58. return this;
  59. }
  60. /**
  61. * Gets the sort criteria to apply to the query. The default is null, which means that the documents will be returned in an undefined
  62. * order.
  63. *
  64. * @return a document describing the sort criteria
  65. * @mongodb.driver.manual reference/method/cursor.sort/ Sort
  66. */
  67. @Nullable
  68. public Bson getSort() {
  69. return sort;
  70. }
  71. /**
  72. * Sets the sort criteria to apply to the query.
  73. *
  74. * @param sort the sort criteria, which may be null.
  75. * @return this
  76. * @mongodb.driver.manual reference/method/cursor.sort/ Sort
  77. */
  78. public FindOneAndDeleteOptions sort(@Nullable final Bson sort) {
  79. this.sort = sort;
  80. return this;
  81. }
  82. /**
  83. * Sets the maximum execution time on the server for this operation.
  84. *
  85. * @param maxTime the max time
  86. * @param timeUnit the time unit, which may not be null
  87. * @return this
  88. */
  89. public FindOneAndDeleteOptions maxTime(final long maxTime, final TimeUnit timeUnit) {
  90. notNull("timeUnit", timeUnit);
  91. this.maxTimeMS = MILLISECONDS.convert(maxTime, timeUnit);
  92. return this;
  93. }
  94. /**
  95. * Gets the maximum execution time for the find one and delete operation.
  96. *
  97. * @param timeUnit the time unit for the result
  98. * @return the max time
  99. */
  100. public long getMaxTime(final TimeUnit timeUnit) {
  101. return timeUnit.convert(maxTimeMS, MILLISECONDS);
  102. }
  103. /**
  104. * Returns the collation options
  105. *
  106. * @return the collation options
  107. * @since 3.4
  108. * @mongodb.server.release 3.4
  109. */
  110. @Nullable
  111. public Collation getCollation() {
  112. return collation;
  113. }
  114. /**
  115. * Sets the collation options
  116. *
  117. * <p>A null value represents the server default.</p>
  118. * @param collation the collation options to use
  119. * @return this
  120. * @since 3.4
  121. * @mongodb.server.release 3.4
  122. */
  123. public FindOneAndDeleteOptions collation(@Nullable final Collation collation) {
  124. this.collation = collation;
  125. return this;
  126. }
  127. /**
  128. * Gets the hint to apply.
  129. *
  130. * @return the hint, which should describe an existing index
  131. * @since 4.1
  132. * @mongodb.server.release 4.4
  133. */
  134. @Nullable
  135. public Bson getHint() {
  136. return hint;
  137. }
  138. /**
  139. * Gets the hint string to apply.
  140. *
  141. * @return the hint string, which should be the name of an existing index
  142. * @since 4.1
  143. * @mongodb.server.release 4.4
  144. */
  145. @Nullable
  146. public String getHintString() {
  147. return hintString;
  148. }
  149. /**
  150. * Sets the hint to apply.
  151. *
  152. * @param hint a document describing the index which should be used for this operation.
  153. * @return this
  154. * @since 4.1
  155. * @mongodb.server.release 4.4
  156. */
  157. public FindOneAndDeleteOptions hint(@Nullable final Bson hint) {
  158. this.hint = hint;
  159. return this;
  160. }
  161. /**
  162. * Sets the hint to apply.
  163. *
  164. * <p>Note: If {@link FindOneAndDeleteOptions#hint(Bson)} is set that will be used instead of any hint string.</p>
  165. *
  166. * @param hint the name of the index which should be used for the operation
  167. * @return this
  168. * @since 4.1
  169. * @mongodb.server.release 4.4
  170. */
  171. public FindOneAndDeleteOptions hintString(@Nullable final String hint) {
  172. this.hintString = hint;
  173. return this;
  174. }
  175. /**
  176. * @return the comment for this operation. A null value means no comment is set.
  177. * @since 4.6
  178. * @mongodb.server.release 4.4
  179. */
  180. @Nullable
  181. public BsonValue getComment() {
  182. return comment;
  183. }
  184. /**
  185. * Sets the comment for this operation. A null value means no comment is set.
  186. *
  187. * @param comment the comment
  188. * @return this
  189. * @since 4.6
  190. * @mongodb.server.release 4.4
  191. */
  192. public FindOneAndDeleteOptions comment(@Nullable final String comment) {
  193. this.comment = comment != null ? new BsonString(comment) : null;
  194. return this;
  195. }
  196. /**
  197. * Sets the comment for this operation. A null value means no comment is set.
  198. *
  199. * @param comment the comment
  200. * @return this
  201. * @since 4.6
  202. * @mongodb.server.release 4.4
  203. */
  204. public FindOneAndDeleteOptions comment(@Nullable final BsonValue comment) {
  205. this.comment = comment;
  206. return this;
  207. }
  208. /**
  209. * Add top-level variables to the operation
  210. *
  211. * @return the top level variables if set or null.
  212. * @mongodb.server.release 5.0
  213. * @since 4.6
  214. */
  215. @Nullable
  216. public Bson getLet() {
  217. return variables;
  218. }
  219. /**
  220. * Add top-level variables for the operation
  221. *
  222. * <p>Allows for improved command readability by separating the variables from the query text.
  223. *
  224. * @param variables for the operation or null
  225. * @return this
  226. * @mongodb.server.release 5.0
  227. * @since 4.6
  228. */
  229. public FindOneAndDeleteOptions let(final Bson variables) {
  230. this.variables = variables;
  231. return this;
  232. }
  233. @Override
  234. public String toString() {
  235. return "FindOneAndDeleteOptions{"
  236. + "projection=" + projection
  237. + ", sort=" + sort
  238. + ", maxTimeMS=" + maxTimeMS
  239. + ", collation=" + collation
  240. + ", hint=" + hint
  241. + ", hintString='" + hintString + '\''
  242. + ", comment=" + comment
  243. + ", let=" + variables
  244. + '}';
  245. }
  246. }