IAggregateFluent.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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.Collections.Generic;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. using MongoDB.Bson;
  20. using MongoDB.Bson.Serialization;
  21. namespace MongoDB.Driver
  22. {
  23. /// <summary>
  24. /// Fluent interface for aggregate.
  25. /// </summary>
  26. /// <remarks>
  27. /// This interface is not guaranteed to remain stable. Implementors should use
  28. /// <see cref="AggregateFluentBase{TResult}" />.
  29. /// </remarks>
  30. /// <typeparam name="TResult">The type of the result of the pipeline.</typeparam>
  31. public interface IAggregateFluent<TResult> : IAsyncCursorSource<TResult>
  32. {
  33. /// <summary>
  34. /// Gets the database.
  35. /// </summary>
  36. IMongoDatabase Database { get; }
  37. /// <summary>
  38. /// Gets the options.
  39. /// </summary>
  40. AggregateOptions Options { get; }
  41. /// <summary>
  42. /// Gets the stages.
  43. /// </summary>
  44. IList<IPipelineStageDefinition> Stages { get; }
  45. /// <summary>
  46. /// Appends the stage to the pipeline.
  47. /// </summary>
  48. /// <typeparam name="TNewResult">The type of the result of the stage.</typeparam>
  49. /// <param name="stage">The stage.</param>
  50. /// <returns>The fluent aggregate interface.</returns>
  51. IAggregateFluent<TNewResult> AppendStage<TNewResult>(PipelineStageDefinition<TResult, TNewResult> stage);
  52. /// <summary>
  53. /// Changes the result type of the pipeline.
  54. /// </summary>
  55. /// <typeparam name="TNewResult">The type of the new result.</typeparam>
  56. /// <param name="newResultSerializer">The new result serializer.</param>
  57. /// <returns>The fluent aggregate interface.</returns>
  58. IAggregateFluent<TNewResult> As<TNewResult>(IBsonSerializer<TNewResult> newResultSerializer = null);
  59. /// <summary>
  60. /// Appends a $bucket stage to the pipeline.
  61. /// </summary>
  62. /// <typeparam name="TValue">The type of the value.</typeparam>
  63. /// <param name="groupBy">The expression providing the value to group by.</param>
  64. /// <param name="boundaries">The bucket boundaries.</param>
  65. /// <param name="options">The options.</param>
  66. /// <returns>The fluent aggregate interface.</returns>
  67. IAggregateFluent<AggregateBucketResult<TValue>> Bucket<TValue>(
  68. AggregateExpressionDefinition<TResult, TValue> groupBy,
  69. IEnumerable<TValue> boundaries,
  70. AggregateBucketOptions<TValue> options = null);
  71. /// <summary>
  72. /// Appends a $bucket stage to the pipeline with a custom projection.
  73. /// </summary>
  74. /// <typeparam name="TValue">The type of the value.</typeparam>
  75. /// <typeparam name="TNewResult">The type of the new result.</typeparam>
  76. /// <param name="groupBy">The expression providing the value to group by.</param>
  77. /// <param name="boundaries">The bucket boundaries.</param>
  78. /// <param name="output">The output projection.</param>
  79. /// <param name="options">The options.</param>
  80. /// <returns>The fluent aggregate interface.</returns>
  81. IAggregateFluent<TNewResult> Bucket<TValue, TNewResult>(
  82. AggregateExpressionDefinition<TResult, TValue> groupBy,
  83. IEnumerable<TValue> boundaries,
  84. ProjectionDefinition<TResult, TNewResult> output,
  85. AggregateBucketOptions<TValue> options = null);
  86. /// <summary>
  87. /// Appends a $bucketAuto stage to the pipeline.
  88. /// </summary>
  89. /// <typeparam name="TValue">The type of the value.</typeparam>
  90. /// <param name="groupBy">The expression providing the value to group by.</param>
  91. /// <param name="buckets">The number of buckets.</param>
  92. /// <param name="options">The options (optional).</param>
  93. /// <returns>The fluent aggregate interface.</returns>
  94. IAggregateFluent<AggregateBucketAutoResult<TValue>> BucketAuto<TValue>(
  95. AggregateExpressionDefinition<TResult, TValue> groupBy,
  96. int buckets,
  97. AggregateBucketAutoOptions options = null);
  98. /// <summary>
  99. /// Appends a $bucketAuto stage to the pipeline with a custom projection.
  100. /// </summary>
  101. /// <typeparam name="TValue">The type of the value.</typeparam>
  102. /// <typeparam name="TNewResult">The type of the new result.</typeparam>
  103. /// <param name="groupBy">The expression providing the value to group by.</param>
  104. /// <param name="buckets">The number of buckets.</param>
  105. /// <param name="output">The output projection.</param>
  106. /// <param name="options">The options (optional).</param>
  107. /// <returns>The fluent aggregate interface.</returns>
  108. IAggregateFluent<TNewResult> BucketAuto<TValue, TNewResult>(
  109. AggregateExpressionDefinition<TResult, TValue> groupBy,
  110. int buckets,
  111. ProjectionDefinition<TResult, TNewResult> output,
  112. AggregateBucketAutoOptions options = null);
  113. /// <summary>
  114. /// Appends a count stage to the pipeline.
  115. /// </summary>
  116. /// <returns>The fluent aggregate interface.</returns>
  117. IAggregateFluent<AggregateCountResult> Count();
  118. /// <summary>
  119. /// Appends a $facet stage to the pipeline.
  120. /// </summary>
  121. /// <typeparam name="TNewResult">The type of the new result.</typeparam>
  122. /// <param name="facets">The facets.</param>
  123. /// <param name="options">The options.</param>
  124. /// <returns>
  125. /// The fluent aggregate interface.
  126. /// </returns>
  127. IAggregateFluent<TNewResult> Facet<TNewResult>(
  128. IEnumerable<AggregateFacet<TResult>> facets,
  129. AggregateFacetOptions<TNewResult> options = null);
  130. /// <summary>
  131. /// Appends a $graphLookup stage to the pipeline.
  132. /// </summary>
  133. /// <typeparam name="TFrom">The type of the from documents.</typeparam>
  134. /// <typeparam name="TConnectFrom">The type of the connect from field (must be either TConnectTo or a type that implements IEnumerable{TConnectTo}).</typeparam>
  135. /// <typeparam name="TConnectTo">The type of the connect to field.</typeparam>
  136. /// <typeparam name="TStartWith">The type of the start with expression (must be either TConnectTo or a type that implements IEnumerable{TConnectTo}).</typeparam>
  137. /// <typeparam name="TAsElement">The type of the as field elements.</typeparam>
  138. /// <typeparam name="TAs">The type of the as field.</typeparam>
  139. /// <typeparam name="TNewResult">The type of the new result (must be same as TResult with an additional as field).</typeparam>
  140. /// <param name="from">The from collection.</param>
  141. /// <param name="connectFromField">The connect from field.</param>
  142. /// <param name="connectToField">The connect to field.</param>
  143. /// <param name="startWith">The start with value.</param>
  144. /// <param name="as">The as field.</param>
  145. /// <param name="depthField">The depth field.</param>
  146. /// <param name="options">The options.</param>
  147. /// <returns>The fluent aggregate interface.</returns>
  148. IAggregateFluent<TNewResult> GraphLookup<TFrom, TConnectFrom, TConnectTo, TStartWith, TAsElement, TAs, TNewResult>(
  149. IMongoCollection<TFrom> from,
  150. FieldDefinition<TFrom, TConnectFrom> connectFromField,
  151. FieldDefinition<TFrom, TConnectTo> connectToField,
  152. AggregateExpressionDefinition<TResult, TStartWith> startWith,
  153. FieldDefinition<TNewResult, TAs> @as,
  154. FieldDefinition<TAsElement, int> depthField,
  155. AggregateGraphLookupOptions<TFrom, TAsElement, TNewResult> options = null)
  156. where TAs : IEnumerable<TAsElement>;
  157. /// <summary>
  158. /// Appends a group stage to the pipeline.
  159. /// </summary>
  160. /// <typeparam name="TNewResult">The type of the result of the stage.</typeparam>
  161. /// <param name="group">The group projection.</param>
  162. /// <returns>The fluent aggregate interface.</returns>
  163. IAggregateFluent<TNewResult> Group<TNewResult>(ProjectionDefinition<TResult, TNewResult> group);
  164. /// <summary>
  165. /// Appends a limit stage to the pipeline.
  166. /// </summary>
  167. /// <param name="limit">The limit.</param>
  168. /// <returns>The fluent aggregate interface.</returns>
  169. IAggregateFluent<TResult> Limit(int limit);
  170. /// <summary>
  171. /// Appends a lookup stage to the pipeline.
  172. /// </summary>
  173. /// <typeparam name="TForeignDocument">The type of the foreign document.</typeparam>
  174. /// <typeparam name="TNewResult">The type of the new result.</typeparam>
  175. /// <param name="foreignCollectionName">Name of the other collection.</param>
  176. /// <param name="localField">The local field.</param>
  177. /// <param name="foreignField">The foreign field.</param>
  178. /// <param name="as">The field in <typeparamref name="TNewResult" /> to place the foreign results.</param>
  179. /// <param name="options">The options.</param>
  180. /// <returns>The fluent aggregate interface.</returns>
  181. IAggregateFluent<TNewResult> Lookup<TForeignDocument, TNewResult>(string foreignCollectionName, FieldDefinition<TResult> localField, FieldDefinition<TForeignDocument> foreignField, FieldDefinition<TNewResult> @as, AggregateLookupOptions<TForeignDocument, TNewResult> options = null);
  182. /// <summary>
  183. /// Appends a match stage to the pipeline.
  184. /// </summary>
  185. /// <param name="filter">The filter.</param>
  186. /// <returns>The fluent aggregate interface.</returns>
  187. IAggregateFluent<TResult> Match(FilterDefinition<TResult> filter);
  188. /// <summary>
  189. /// Appends a match stage to the pipeline that matches derived documents and changes the result type to the derived type.
  190. /// </summary>
  191. /// <typeparam name="TNewResult">The type of the derived documents.</typeparam>
  192. /// <param name="newResultSerializer">The new result serializer.</param>
  193. /// <returns>The fluent aggregate interface.</returns>
  194. IAggregateFluent<TNewResult> OfType<TNewResult>(IBsonSerializer<TNewResult> newResultSerializer = null) where TNewResult : TResult;
  195. /// <summary>
  196. /// Appends an out stage to the pipeline and executes it, and then returns a cursor to read the contents of the output collection.
  197. /// </summary>
  198. /// <param name="collectionName">Name of the collection.</param>
  199. /// <param name="cancellationToken">The cancellation token.</param>
  200. /// <returns>A cursor.</returns>
  201. IAsyncCursor<TResult> Out(string collectionName, CancellationToken cancellationToken = default(CancellationToken));
  202. /// <summary>
  203. /// Appends an out stage to the pipeline and executes it, and then returns a cursor to read the contents of the output collection.
  204. /// </summary>
  205. /// <param name="collectionName">Name of the collection.</param>
  206. /// <param name="cancellationToken">The cancellation token.</param>
  207. /// <returns>A Task whose result is a cursor.</returns>
  208. Task<IAsyncCursor<TResult>> OutAsync(string collectionName, CancellationToken cancellationToken = default(CancellationToken));
  209. /// <summary>
  210. /// Appends a project stage to the pipeline.
  211. /// </summary>
  212. /// <typeparam name="TNewResult">The type of the result of the stage.</typeparam>
  213. /// <param name="projection">The projection.</param>
  214. /// <returns>
  215. /// The fluent aggregate interface.
  216. /// </returns>
  217. IAggregateFluent<TNewResult> Project<TNewResult>(ProjectionDefinition<TResult, TNewResult> projection);
  218. /// <summary>
  219. /// Appends a $replaceRoot stage to the pipeline.
  220. /// </summary>
  221. /// <typeparam name="TNewResult">The type of the new result.</typeparam>
  222. /// <param name="newRoot">The new root.</param>
  223. /// <returns>The fluent aggregate interface.</returns>
  224. IAggregateFluent<TNewResult> ReplaceRoot<TNewResult>(AggregateExpressionDefinition<TResult, TNewResult> newRoot);
  225. /// <summary>
  226. /// Appends a skip stage to the pipeline.
  227. /// </summary>
  228. /// <param name="skip">The number of documents to skip.</param>
  229. /// <returns>The fluent aggregate interface.</returns>
  230. IAggregateFluent<TResult> Skip(int skip);
  231. /// <summary>
  232. /// Appends a sort stage to the pipeline.
  233. /// </summary>
  234. /// <param name="sort">The sort specification.</param>
  235. /// <returns>The fluent aggregate interface.</returns>
  236. IAggregateFluent<TResult> Sort(SortDefinition<TResult> sort);
  237. /// <summary>
  238. /// Appends a sortByCount stage to the pipeline.
  239. /// </summary>
  240. /// <typeparam name="TId">The type of the identifier.</typeparam>
  241. /// <param name="id">The identifier.</param>
  242. /// <returns>The fluent aggregate interface.</returns>
  243. IAggregateFluent<AggregateSortByCountResult<TId>> SortByCount<TId>(AggregateExpressionDefinition<TResult, TId> id);
  244. /// <summary>
  245. /// Appends an unwind stage to the pipeline.
  246. /// </summary>
  247. /// <typeparam name="TNewResult">The type of the result of the stage.</typeparam>
  248. /// <param name="field">The field.</param>
  249. /// <param name="newResultSerializer">The new result serializer.</param>
  250. /// <returns>
  251. /// The fluent aggregate interface.
  252. /// </returns>
  253. [Obsolete("Use the Unwind overload which takes an options parameter.")]
  254. IAggregateFluent<TNewResult> Unwind<TNewResult>(FieldDefinition<TResult> field, IBsonSerializer<TNewResult> newResultSerializer);
  255. /// <summary>
  256. /// Appends an unwind stage to the pipeline.
  257. /// </summary>
  258. /// <typeparam name="TNewResult">The type of the new result.</typeparam>
  259. /// <param name="field">The field.</param>
  260. /// <param name="options">The options.</param>
  261. /// <returns>The fluent aggregate interface.</returns>
  262. IAggregateFluent<TNewResult> Unwind<TNewResult>(FieldDefinition<TResult> field, AggregateUnwindOptions<TNewResult> options = null);
  263. }
  264. /// <summary>
  265. /// Fluent interface for aggregate.
  266. /// </summary>
  267. /// <typeparam name="TResult">The type of the result.</typeparam>
  268. public interface IOrderedAggregateFluent<TResult> : IAggregateFluent<TResult>
  269. {
  270. /// <summary>
  271. /// Combines the current sort definition with an additional sort definition.
  272. /// </summary>
  273. /// <param name="newSort">The new sort.</param>
  274. /// <returns>The fluent aggregate interface.</returns>
  275. IOrderedAggregateFluent<TResult> ThenBy(SortDefinition<TResult> newSort);
  276. }
  277. }