FindOptions.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* Copyright 2015-present 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. */
  15. using System;
  16. using MongoDB.Bson;
  17. using MongoDB.Driver.Core.Misc;
  18. namespace MongoDB.Driver
  19. {
  20. /// <summary>
  21. /// Options for a find operation.
  22. /// </summary>
  23. public abstract class FindOptionsBase
  24. {
  25. // fields
  26. private bool? _allowPartialResults;
  27. private int? _batchSize;
  28. private Collation _collation;
  29. private string _comment;
  30. private CursorType _cursorType;
  31. private TimeSpan? _maxAwaitTime;
  32. private TimeSpan? _maxTime;
  33. private BsonDocument _modifiers;
  34. private bool? _noCursorTimeout;
  35. private bool? _oplogReplay;
  36. // constructors
  37. /// <summary>
  38. /// Initializes a new instance of the <see cref="FindOptionsBase"/> class.
  39. /// </summary>
  40. public FindOptionsBase()
  41. {
  42. _cursorType = CursorType.NonTailable;
  43. }
  44. // properties
  45. /// <summary>
  46. /// Gets or sets a value indicating whether to allow partial results when some shards are unavailable.
  47. /// </summary>
  48. public bool? AllowPartialResults
  49. {
  50. get { return _allowPartialResults; }
  51. set { _allowPartialResults = value; }
  52. }
  53. /// <summary>
  54. /// Gets or sets the size of a batch.
  55. /// </summary>
  56. public int? BatchSize
  57. {
  58. get { return _batchSize; }
  59. set { _batchSize = Ensure.IsNullOrGreaterThanOrEqualToZero(value, nameof(value)); }
  60. }
  61. /// <summary>
  62. /// Gets or sets the collation.
  63. /// </summary>
  64. public Collation Collation
  65. {
  66. get { return _collation; }
  67. set { _collation = value; }
  68. }
  69. /// <summary>
  70. /// Gets or sets the comment.
  71. /// </summary>
  72. public string Comment
  73. {
  74. get { return _comment; }
  75. set { _comment = value; }
  76. }
  77. /// <summary>
  78. /// Gets or sets the type of the cursor.
  79. /// </summary>
  80. public CursorType CursorType
  81. {
  82. get { return _cursorType; }
  83. set { _cursorType = value; }
  84. }
  85. /// <summary>
  86. /// Gets or sets the maximum await time for TailableAwait cursors.
  87. /// </summary>
  88. public TimeSpan? MaxAwaitTime
  89. {
  90. get { return _maxAwaitTime; }
  91. set { _maxAwaitTime = value; }
  92. }
  93. /// <summary>
  94. /// Gets or sets the maximum time.
  95. /// </summary>
  96. public TimeSpan? MaxTime
  97. {
  98. get { return _maxTime; }
  99. set { _maxTime = Ensure.IsNullOrInfiniteOrGreaterThanOrEqualToZero(value, nameof(value)); }
  100. }
  101. /// <summary>
  102. /// Gets or sets the modifiers.
  103. /// </summary>
  104. public BsonDocument Modifiers
  105. {
  106. get { return _modifiers; }
  107. set { _modifiers = value; }
  108. }
  109. /// <summary>
  110. /// Gets or sets whether a cursor will time out.
  111. /// </summary>
  112. public bool? NoCursorTimeout
  113. {
  114. get { return _noCursorTimeout; }
  115. set { _noCursorTimeout = value; }
  116. }
  117. /// <summary>
  118. /// Gets or sets whether the OplogReplay bit will be set.
  119. /// </summary>
  120. public bool? OplogReplay
  121. {
  122. get { return _oplogReplay; }
  123. set { _oplogReplay = value; }
  124. }
  125. }
  126. /// <summary>
  127. /// Options for finding documents.
  128. /// </summary>
  129. public class FindOptions : FindOptionsBase
  130. { }
  131. /// <summary>
  132. /// Options for finding documents.
  133. /// </summary>
  134. /// <typeparam name="TDocument">The type of the document.</typeparam>
  135. /// <typeparam name="TProjection">The type of the projection (same as TDocument if there is no projection).</typeparam>
  136. public class FindOptions<TDocument, TProjection> : FindOptionsBase
  137. {
  138. // fields
  139. private int? _limit;
  140. private ProjectionDefinition<TDocument, TProjection> _projection;
  141. private int? _skip;
  142. private SortDefinition<TDocument> _sort;
  143. // properties
  144. /// <summary>
  145. /// Gets or sets how many documents to return.
  146. /// </summary>
  147. public int? Limit
  148. {
  149. get { return _limit; }
  150. set { _limit = value; }
  151. }
  152. /// <summary>
  153. /// Gets or sets the projection.
  154. /// </summary>
  155. public ProjectionDefinition<TDocument, TProjection> Projection
  156. {
  157. get { return _projection; }
  158. set { _projection = value; }
  159. }
  160. /// <summary>
  161. /// Gets or sets how many documents to skip before returning the rest.
  162. /// </summary>
  163. public int? Skip
  164. {
  165. get { return _skip; }
  166. set { _skip = value; }
  167. }
  168. /// <summary>
  169. /// Gets or sets the sort.
  170. /// </summary>
  171. public SortDefinition<TDocument> Sort
  172. {
  173. get { return _sort; }
  174. set { _sort = value; }
  175. }
  176. }
  177. /// <summary>
  178. /// Options for finding documents.
  179. /// </summary>
  180. /// <typeparam name="TDocument">The type of the document and the result.</typeparam>
  181. public class FindOptions<TDocument> : FindOptions<TDocument, TDocument>
  182. {
  183. }
  184. }