FindFluent.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* Copyright 2010-2016 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. // constructors
  31. public FindFluent(IMongoCollection<TDocument> collection, FilterDefinition<TDocument> filter, FindOptions<TDocument, TProjection> options)
  32. {
  33. _collection = Ensure.IsNotNull(collection, nameof(collection));
  34. _filter = Ensure.IsNotNull(filter, nameof(filter));
  35. _options = Ensure.IsNotNull(options, nameof(options));
  36. }
  37. // public properties
  38. public override FilterDefinition<TDocument> Filter
  39. {
  40. get { return _filter; }
  41. set { _filter = Ensure.IsNotNull(value, nameof(value)); }
  42. }
  43. public override FindOptions<TDocument, TProjection> Options
  44. {
  45. get { return _options; }
  46. }
  47. // public methods
  48. public override IFindFluent<TDocument, TResult> As<TResult>(IBsonSerializer<TResult> resultSerializer)
  49. {
  50. var projection = Builders<TDocument>.Projection.As<TResult>(resultSerializer);
  51. return Project(projection);
  52. }
  53. public override long Count(CancellationToken cancellationToken)
  54. {
  55. var options = CreateCountOptions();
  56. return _collection.Count(_filter, options, cancellationToken);
  57. }
  58. public override Task<long> CountAsync(CancellationToken cancellationToken)
  59. {
  60. var options = CreateCountOptions();
  61. return _collection.CountAsync(_filter, options, cancellationToken);
  62. }
  63. public override IFindFluent<TDocument, TProjection> Limit(int? limit)
  64. {
  65. _options.Limit = limit;
  66. return this;
  67. }
  68. public override IFindFluent<TDocument, TNewProjection> Project<TNewProjection>(ProjectionDefinition<TDocument, TNewProjection> projection)
  69. {
  70. var newOptions = new FindOptions<TDocument, TNewProjection>
  71. {
  72. AllowPartialResults = _options.AllowPartialResults,
  73. BatchSize = _options.BatchSize,
  74. Collation = _options.Collation,
  75. Comment = _options.Comment,
  76. CursorType = _options.CursorType,
  77. Limit = _options.Limit,
  78. MaxAwaitTime = _options.MaxAwaitTime,
  79. MaxTime = _options.MaxTime,
  80. Modifiers = _options.Modifiers,
  81. NoCursorTimeout = _options.NoCursorTimeout,
  82. OplogReplay = _options.OplogReplay,
  83. Projection = projection,
  84. Skip = _options.Skip,
  85. Sort = _options.Sort,
  86. };
  87. return new FindFluent<TDocument, TNewProjection>(_collection, _filter, newOptions);
  88. }
  89. public override IFindFluent<TDocument, TProjection> Skip(int? skip)
  90. {
  91. _options.Skip = skip;
  92. return this;
  93. }
  94. public override IFindFluent<TDocument, TProjection> Sort(SortDefinition<TDocument> sort)
  95. {
  96. _options.Sort = sort;
  97. return this;
  98. }
  99. public override IAsyncCursor<TProjection> ToCursor(CancellationToken cancellationToken = default(CancellationToken))
  100. {
  101. return _collection.FindSync(_filter, _options, cancellationToken);
  102. }
  103. public override Task<IAsyncCursor<TProjection>> ToCursorAsync(CancellationToken cancellationToken = default(CancellationToken))
  104. {
  105. return _collection.FindAsync(_filter, _options, cancellationToken);
  106. }
  107. public override string ToString()
  108. {
  109. var sb = new StringBuilder("find(");
  110. var renderedFilter = Render(_filter.Render);
  111. sb.Append(renderedFilter.ToString());
  112. if (_options.Projection != null)
  113. {
  114. var renderedProjection = Render(_options.Projection.Render);
  115. if (renderedProjection.Document != null)
  116. {
  117. sb.Append(", " + renderedProjection.Document.ToString());
  118. }
  119. }
  120. sb.Append(")");
  121. if (_options.Collation != null)
  122. {
  123. sb.Append(".collation(" + _options.Collation.ToString() + ")");
  124. }
  125. if (_options.Sort != null)
  126. {
  127. var renderedSort = Render(_options.Sort.Render);
  128. sb.Append(".sort(" + renderedSort.ToString() + ")");
  129. }
  130. if (_options.Skip.HasValue)
  131. {
  132. sb.Append(".skip(" + _options.Skip.Value.ToString() + ")");
  133. }
  134. if (_options.Limit.HasValue)
  135. {
  136. sb.Append(".limit(" + _options.Limit.Value.ToString() + ")");
  137. }
  138. if (_options.MaxTime != null)
  139. {
  140. sb.Append(".maxTime(" + _options.MaxTime.Value.TotalMilliseconds + ")");
  141. }
  142. if (_options.Comment != null)
  143. {
  144. sb.Append("._addSpecial(\"$comment\", \"" + _options.Comment + "\")");
  145. }
  146. if (_options.Modifiers != null)
  147. {
  148. foreach (var modifier in _options.Modifiers)
  149. {
  150. sb.Append("._addSpecial(\"" + modifier.Name + "\", ");
  151. if (modifier.Value.BsonType == BsonType.String)
  152. {
  153. sb.Append("\"" + modifier.Value.ToString() + "\"");
  154. }
  155. else
  156. {
  157. sb.Append(modifier.Value.ToString());
  158. }
  159. sb.Append(")");
  160. }
  161. }
  162. return sb.ToString();
  163. }
  164. // private methods
  165. private CountOptions CreateCountOptions()
  166. {
  167. BsonValue hint = null;
  168. if (_options.Modifiers != null)
  169. {
  170. _options.Modifiers.TryGetValue("$hint", out hint);
  171. }
  172. return new CountOptions
  173. {
  174. Collation = _options.Collation,
  175. Hint = hint,
  176. Limit = _options.Limit,
  177. MaxTime = _options.MaxTime,
  178. Skip = _options.Skip
  179. };
  180. }
  181. private TRendered Render<TRendered>(Func<IBsonSerializer<TDocument>, IBsonSerializerRegistry, TRendered> renderer)
  182. {
  183. return renderer(_collection.DocumentSerializer, _collection.Settings.SerializerRegistry);
  184. }
  185. }
  186. }