FindFluent.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 System.Text;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. using MongoDB.Bson;
  20. using MongoDB.Bson.Serialization;
  21. using MongoDB.Driver.Core.Misc;
  22. namespace MongoDB.Driver
  23. {
  24. internal class FindFluent<TDocument, TProjection> : FindFluentBase<TDocument, TProjection>
  25. {
  26. // private fields
  27. private readonly IMongoCollection<TDocument> _collection;
  28. private FilterDefinition<TDocument> _filter;
  29. private readonly FindOptions<TDocument, TProjection> _options;
  30. private readonly IClientSessionHandle _session;
  31. // constructors
  32. public FindFluent(IClientSessionHandle session, IMongoCollection<TDocument> collection, FilterDefinition<TDocument> filter, FindOptions<TDocument, TProjection> options)
  33. {
  34. _session = session; // can be null
  35. _collection = Ensure.IsNotNull(collection, nameof(collection));
  36. _filter = Ensure.IsNotNull(filter, nameof(filter));
  37. _options = Ensure.IsNotNull(options, nameof(options));
  38. }
  39. // public properties
  40. public override FilterDefinition<TDocument> Filter
  41. {
  42. get { return _filter; }
  43. set { _filter = Ensure.IsNotNull(value, nameof(value)); }
  44. }
  45. public override FindOptions<TDocument, TProjection> Options
  46. {
  47. get { return _options; }
  48. }
  49. // public methods
  50. public override IFindFluent<TDocument, TResult> As<TResult>(IBsonSerializer<TResult> resultSerializer)
  51. {
  52. var projection = Builders<TDocument>.Projection.As<TResult>(resultSerializer);
  53. return Project(projection);
  54. }
  55. [Obsolete("Use CountDocuments instead.")]
  56. public override long Count(CancellationToken cancellationToken)
  57. {
  58. var options = CreateCountOptions();
  59. if (_session == null)
  60. {
  61. return _collection.Count(_filter, options, cancellationToken);
  62. }
  63. else
  64. {
  65. return _collection.Count(_session, _filter, options, cancellationToken);
  66. }
  67. }
  68. [Obsolete("Use CountDocumentsAsync instead.")]
  69. public override Task<long> CountAsync(CancellationToken cancellationToken)
  70. {
  71. var options = CreateCountOptions();
  72. if (_session == null)
  73. {
  74. return _collection.CountAsync(_filter, options, cancellationToken);
  75. }
  76. else
  77. {
  78. return _collection.CountAsync(_session, _filter, options, cancellationToken);
  79. }
  80. }
  81. public override long CountDocuments(CancellationToken cancellationToken)
  82. {
  83. var options = CreateCountOptions();
  84. if (_session == null)
  85. {
  86. return _collection.CountDocuments(_filter, options, cancellationToken);
  87. }
  88. else
  89. {
  90. return _collection.CountDocuments(_session, _filter, options, cancellationToken);
  91. }
  92. }
  93. public override Task<long> CountDocumentsAsync(CancellationToken cancellationToken)
  94. {
  95. var options = CreateCountOptions();
  96. if (_session == null)
  97. {
  98. return _collection.CountDocumentsAsync(_filter, options, cancellationToken);
  99. }
  100. else
  101. {
  102. return _collection.CountDocumentsAsync(_session, _filter, options, cancellationToken);
  103. }
  104. }
  105. public override IFindFluent<TDocument, TProjection> Limit(int? limit)
  106. {
  107. _options.Limit = limit;
  108. return this;
  109. }
  110. public override IFindFluent<TDocument, TNewProjection> Project<TNewProjection>(ProjectionDefinition<TDocument, TNewProjection> projection)
  111. {
  112. var newOptions = new FindOptions<TDocument, TNewProjection>
  113. {
  114. AllowPartialResults = _options.AllowPartialResults,
  115. BatchSize = _options.BatchSize,
  116. Collation = _options.Collation,
  117. Comment = _options.Comment,
  118. CursorType = _options.CursorType,
  119. Limit = _options.Limit,
  120. MaxAwaitTime = _options.MaxAwaitTime,
  121. MaxTime = _options.MaxTime,
  122. Modifiers = _options.Modifiers,
  123. NoCursorTimeout = _options.NoCursorTimeout,
  124. OplogReplay = _options.OplogReplay,
  125. Projection = projection,
  126. Skip = _options.Skip,
  127. Sort = _options.Sort,
  128. };
  129. return new FindFluent<TDocument, TNewProjection>(_session, _collection, _filter, newOptions);
  130. }
  131. public override IFindFluent<TDocument, TProjection> Skip(int? skip)
  132. {
  133. _options.Skip = skip;
  134. return this;
  135. }
  136. public override IFindFluent<TDocument, TProjection> Sort(SortDefinition<TDocument> sort)
  137. {
  138. _options.Sort = sort;
  139. return this;
  140. }
  141. public override IAsyncCursor<TProjection> ToCursor(CancellationToken cancellationToken = default(CancellationToken))
  142. {
  143. if (_session == null)
  144. {
  145. return _collection.FindSync(_filter, _options, cancellationToken);
  146. }
  147. else
  148. {
  149. return _collection.FindSync(_session, _filter, _options, cancellationToken);
  150. }
  151. }
  152. public override Task<IAsyncCursor<TProjection>> ToCursorAsync(CancellationToken cancellationToken = default(CancellationToken))
  153. {
  154. if (_session == null)
  155. {
  156. return _collection.FindAsync(_filter, _options, cancellationToken);
  157. }
  158. else
  159. {
  160. return _collection.FindAsync(_session, _filter, _options, cancellationToken);
  161. }
  162. }
  163. public override string ToString()
  164. {
  165. var sb = new StringBuilder("find(");
  166. var renderedFilter = Render(_filter.Render);
  167. sb.Append(renderedFilter.ToString());
  168. if (_options.Projection != null)
  169. {
  170. var renderedProjection = Render(_options.Projection.Render);
  171. if (renderedProjection.Document != null)
  172. {
  173. sb.Append(", " + renderedProjection.Document.ToString());
  174. }
  175. }
  176. sb.Append(")");
  177. if (_options.Collation != null)
  178. {
  179. sb.Append(".collation(" + _options.Collation.ToString() + ")");
  180. }
  181. if (_options.Sort != null)
  182. {
  183. var renderedSort = Render(_options.Sort.Render);
  184. sb.Append(".sort(" + renderedSort.ToString() + ")");
  185. }
  186. if (_options.Skip.HasValue)
  187. {
  188. sb.Append(".skip(" + _options.Skip.Value.ToString() + ")");
  189. }
  190. if (_options.Limit.HasValue)
  191. {
  192. sb.Append(".limit(" + _options.Limit.Value.ToString() + ")");
  193. }
  194. if (_options.MaxTime != null)
  195. {
  196. sb.Append(".maxTime(" + _options.MaxTime.Value.TotalMilliseconds + ")");
  197. }
  198. if (_options.Comment != null)
  199. {
  200. sb.Append("._addSpecial(\"$comment\", \"" + _options.Comment + "\")");
  201. }
  202. if (_options.Modifiers != null)
  203. {
  204. foreach (var modifier in _options.Modifiers)
  205. {
  206. sb.Append("._addSpecial(\"" + modifier.Name + "\", ");
  207. if (modifier.Value.BsonType == BsonType.String)
  208. {
  209. sb.Append("\"" + modifier.Value.ToString() + "\"");
  210. }
  211. else
  212. {
  213. sb.Append(modifier.Value.ToString());
  214. }
  215. sb.Append(")");
  216. }
  217. }
  218. return sb.ToString();
  219. }
  220. // private methods
  221. private CountOptions CreateCountOptions()
  222. {
  223. return new CountOptions
  224. {
  225. Collation = _options.Collation,
  226. Hint = _options.Modifiers?.GetValue("$hint", null),
  227. Limit = _options.Limit,
  228. MaxTime = _options.MaxTime,
  229. Skip = _options.Skip
  230. };
  231. }
  232. private TRendered Render<TRendered>(Func<IBsonSerializer<TDocument>, IBsonSerializerRegistry, TRendered> renderer)
  233. {
  234. return renderer(_collection.DocumentSerializer, _collection.Settings.SerializerRegistry);
  235. }
  236. }
  237. }