CreateIndexOptions.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* Copyright 2010-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. namespace MongoDB.Driver
  18. {
  19. /// <summary>
  20. /// Options for creating an index.
  21. /// </summary>
  22. public class CreateIndexOptions
  23. {
  24. // fields
  25. private bool? _background;
  26. private int? _bits;
  27. private double? _bucketSize;
  28. private Collation _collation;
  29. private string _defaultLanguage;
  30. private TimeSpan? _expireAfter;
  31. private string _languageOverride;
  32. private double? _max;
  33. private double? _min;
  34. private string _name;
  35. private bool? _sparse;
  36. private int? _sphereIndexVersion;
  37. private BsonDocument _storageEngine;
  38. private int? _textIndexVersion;
  39. private bool? _unique;
  40. private int? _version;
  41. private BsonDocument _weights;
  42. // properties
  43. /// <summary>
  44. /// Gets or sets a value indicating whether to create the index in the background.
  45. /// </summary>
  46. public bool? Background
  47. {
  48. get { return _background; }
  49. set { _background = value; }
  50. }
  51. /// <summary>
  52. /// Gets or sets the precision, in bits, used with geohash indexes.
  53. /// </summary>
  54. public int? Bits
  55. {
  56. get { return _bits; }
  57. set { _bits = value; }
  58. }
  59. /// <summary>
  60. /// Gets or sets the size of a geohash bucket.
  61. /// </summary>
  62. public double? BucketSize
  63. {
  64. get { return _bucketSize; }
  65. set { _bucketSize = value; }
  66. }
  67. /// <summary>
  68. /// Gets or sets the collation.
  69. /// </summary>
  70. public Collation Collation
  71. {
  72. get { return _collation; }
  73. set { _collation = value; }
  74. }
  75. /// <summary>
  76. /// Gets or sets the default language.
  77. /// </summary>
  78. public string DefaultLanguage
  79. {
  80. get { return _defaultLanguage; }
  81. set { _defaultLanguage = value; }
  82. }
  83. /// <summary>
  84. /// Gets or sets when documents expire (used with TTL indexes).
  85. /// </summary>
  86. public TimeSpan? ExpireAfter
  87. {
  88. get { return _expireAfter; }
  89. set { _expireAfter = value; }
  90. }
  91. /// <summary>
  92. /// Gets or sets the language override.
  93. /// </summary>
  94. public string LanguageOverride
  95. {
  96. get { return _languageOverride; }
  97. set { _languageOverride = value; }
  98. }
  99. /// <summary>
  100. /// Gets or sets the max value for 2d indexes.
  101. /// </summary>
  102. public double? Max
  103. {
  104. get { return _max; }
  105. set { _max = value; }
  106. }
  107. /// <summary>
  108. /// Gets or sets the min value for 2d indexes.
  109. /// </summary>
  110. public double? Min
  111. {
  112. get { return _min; }
  113. set { _min = value; }
  114. }
  115. /// <summary>
  116. /// Gets or sets the index name.
  117. /// </summary>
  118. public string Name
  119. {
  120. get { return _name; }
  121. set { _name = value; }
  122. }
  123. /// <summary>
  124. /// Gets or sets a value indicating whether the index is a sparse index.
  125. /// </summary>
  126. public bool? Sparse
  127. {
  128. get { return _sparse; }
  129. set { _sparse = value; }
  130. }
  131. /// <summary>
  132. /// Gets or sets the index version for 2dsphere indexes.
  133. /// </summary>
  134. public int? SphereIndexVersion
  135. {
  136. get { return _sphereIndexVersion; }
  137. set { _sphereIndexVersion = value; }
  138. }
  139. /// <summary>
  140. /// Gets or sets the storage engine options.
  141. /// </summary>
  142. public BsonDocument StorageEngine
  143. {
  144. get { return _storageEngine; }
  145. set { _storageEngine = value; }
  146. }
  147. /// <summary>
  148. /// Gets or sets the index version for text indexes.
  149. /// </summary>
  150. public int? TextIndexVersion
  151. {
  152. get { return _textIndexVersion; }
  153. set { _textIndexVersion = value; }
  154. }
  155. /// <summary>
  156. /// Gets or sets a value indicating whether the index is a unique index.
  157. /// </summary>
  158. public bool? Unique
  159. {
  160. get { return _unique; }
  161. set { _unique = value; }
  162. }
  163. /// <summary>
  164. /// Gets or sets the version of the index.
  165. /// </summary>
  166. public int? Version
  167. {
  168. get { return _version; }
  169. set { _version = value; }
  170. }
  171. /// <summary>
  172. /// Gets or sets the weights for text indexes.
  173. /// </summary>
  174. public BsonDocument Weights
  175. {
  176. get { return _weights; }
  177. set { _weights = value; }
  178. }
  179. }
  180. /// <summary>
  181. /// Options for creating an index.
  182. /// </summary>
  183. /// <typeparam name="TDocument">The type of the document.</typeparam>
  184. public class CreateIndexOptions<TDocument> : CreateIndexOptions
  185. {
  186. #region static
  187. // internal static methods
  188. internal static CreateIndexOptions<TDocument> CoercedFrom(CreateIndexOptions options)
  189. {
  190. if (options == null)
  191. {
  192. return null;
  193. }
  194. if (options.GetType() == typeof(CreateIndexOptions))
  195. {
  196. return new CreateIndexOptions<TDocument>
  197. {
  198. Background = options.Background,
  199. Bits = options.Bits,
  200. BucketSize = options.BucketSize,
  201. Collation = options.Collation,
  202. DefaultLanguage = options.DefaultLanguage,
  203. ExpireAfter = options.ExpireAfter,
  204. LanguageOverride = options.LanguageOverride,
  205. Max = options.Max,
  206. Min = options.Min,
  207. Name = options.Name,
  208. Sparse = options.Sparse,
  209. SphereIndexVersion = options.SphereIndexVersion,
  210. StorageEngine = options.StorageEngine,
  211. TextIndexVersion = options.TextIndexVersion,
  212. Unique = options.Unique,
  213. Version = options.Version,
  214. Weights = options.Weights
  215. };
  216. }
  217. return (CreateIndexOptions<TDocument>)options;
  218. }
  219. #endregion
  220. // private fields
  221. private FilterDefinition<TDocument> _partialFilterExpression;
  222. // public properties
  223. /// <summary>
  224. /// Gets or sets the partial filter expression.
  225. /// </summary>
  226. public FilterDefinition<TDocument> PartialFilterExpression
  227. {
  228. get { return _partialFilterExpression; }
  229. set { _partialFilterExpression = value; }
  230. }
  231. }
  232. }