IAggregateFluent.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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.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 $changeStream stage to the pipeline.
  115. /// Normally you would prefer to use the Watch method of <see cref="IMongoCollection{TDocument}" />.
  116. /// Only use this method if subsequent stages project away the resume token (the _id)
  117. /// or you don't want the resulting cursor to automatically resume.
  118. /// </summary>
  119. /// <param name="options">The options.</param>
  120. /// <returns>The fluent aggregate interface.</returns>
  121. IAggregateFluent<ChangeStreamDocument<TResult>> ChangeStream(ChangeStreamStageOptions options = null);
  122. /// <summary>
  123. /// Appends a count stage to the pipeline.
  124. /// </summary>
  125. /// <returns>The fluent aggregate interface.</returns>
  126. IAggregateFluent<AggregateCountResult> Count();
  127. /// <summary>
  128. /// Appends a $facet stage to the pipeline.
  129. /// </summary>
  130. /// <typeparam name="TNewResult">The type of the new result.</typeparam>
  131. /// <param name="facets">The facets.</param>
  132. /// <param name="options">The options.</param>
  133. /// <returns>
  134. /// The fluent aggregate interface.
  135. /// </returns>
  136. IAggregateFluent<TNewResult> Facet<TNewResult>(
  137. IEnumerable<AggregateFacet<TResult>> facets,
  138. AggregateFacetOptions<TNewResult> options = null);
  139. /// <summary>
  140. /// Appends a $graphLookup stage to the pipeline.
  141. /// </summary>
  142. /// <typeparam name="TFrom">The type of the from documents.</typeparam>
  143. /// <typeparam name="TConnectFrom">The type of the connect from field (must be either TConnectTo or a type that implements IEnumerable{TConnectTo}).</typeparam>
  144. /// <typeparam name="TConnectTo">The type of the connect to field.</typeparam>
  145. /// <typeparam name="TStartWith">The type of the start with expression (must be either TConnectTo or a type that implements IEnumerable{TConnectTo}).</typeparam>
  146. /// <typeparam name="TAsElement">The type of the as field elements.</typeparam>
  147. /// <typeparam name="TAs">The type of the as field.</typeparam>
  148. /// <typeparam name="TNewResult">The type of the new result (must be same as TResult with an additional as field).</typeparam>
  149. /// <param name="from">The from collection.</param>
  150. /// <param name="connectFromField">The connect from field.</param>
  151. /// <param name="connectToField">The connect to field.</param>
  152. /// <param name="startWith">The start with value.</param>
  153. /// <param name="as">The as field.</param>
  154. /// <param name="depthField">The depth field.</param>
  155. /// <param name="options">The options.</param>
  156. /// <returns>The fluent aggregate interface.</returns>
  157. IAggregateFluent<TNewResult> GraphLookup<TFrom, TConnectFrom, TConnectTo, TStartWith, TAsElement, TAs, TNewResult>(
  158. IMongoCollection<TFrom> from,
  159. FieldDefinition<TFrom, TConnectFrom> connectFromField,
  160. FieldDefinition<TFrom, TConnectTo> connectToField,
  161. AggregateExpressionDefinition<TResult, TStartWith> startWith,
  162. FieldDefinition<TNewResult, TAs> @as,
  163. FieldDefinition<TAsElement, int> depthField,
  164. AggregateGraphLookupOptions<TFrom, TAsElement, TNewResult> options = null)
  165. where TAs : IEnumerable<TAsElement>;
  166. /// <summary>
  167. /// Appends a group stage to the pipeline.
  168. /// </summary>
  169. /// <typeparam name="TNewResult">The type of the result of the stage.</typeparam>
  170. /// <param name="group">The group projection.</param>
  171. /// <returns>The fluent aggregate interface.</returns>
  172. IAggregateFluent<TNewResult> Group<TNewResult>(ProjectionDefinition<TResult, TNewResult> group);
  173. /// <summary>
  174. /// Appends a limit stage to the pipeline.
  175. /// </summary>
  176. /// <param name="limit">The limit.</param>
  177. /// <returns>The fluent aggregate interface.</returns>
  178. IAggregateFluent<TResult> Limit(int limit);
  179. /// <summary>
  180. /// Appends a lookup stage to the pipeline.
  181. /// </summary>
  182. /// <typeparam name="TForeignDocument">The type of the foreign document.</typeparam>
  183. /// <typeparam name="TNewResult">The type of the new result.</typeparam>
  184. /// <param name="foreignCollectionName">Name of the other collection.</param>
  185. /// <param name="localField">The local field.</param>
  186. /// <param name="foreignField">The foreign field.</param>
  187. /// <param name="as">The field in <typeparamref name="TNewResult" /> to place the foreign results.</param>
  188. /// <param name="options">The options.</param>
  189. /// <returns>The fluent aggregate interface.</returns>
  190. IAggregateFluent<TNewResult> Lookup<TForeignDocument, TNewResult>(string foreignCollectionName, FieldDefinition<TResult> localField, FieldDefinition<TForeignDocument> foreignField, FieldDefinition<TNewResult> @as, AggregateLookupOptions<TForeignDocument, TNewResult> options = null);
  191. /// <summary>
  192. /// Appends a lookup stage to the pipeline.
  193. /// </summary>
  194. /// <typeparam name="TForeignDocument">The type of the foreign collection documents.</typeparam>
  195. /// <typeparam name="TAsElement">The type of the as field elements.</typeparam>
  196. /// <typeparam name="TAs">The type of the as field.</typeparam>
  197. /// <typeparam name="TNewResult">The type of the new result.</typeparam>
  198. /// <param name="foreignCollection">The foreign collection.</param>
  199. /// <param name="let">The "let" definition.</param>
  200. /// <param name="lookupPipeline">The lookup pipeline.</param>
  201. /// <param name="as">The as field in <typeparamref name="TNewResult" /> in which to place the results of the lookup pipeline.</param>
  202. /// <param name="options">The options.</param>
  203. /// <returns>The fluent aggregate interface.</returns>
  204. IAggregateFluent<TNewResult> Lookup<TForeignDocument, TAsElement, TAs, TNewResult>(
  205. IMongoCollection<TForeignDocument> foreignCollection,
  206. BsonDocument let,
  207. PipelineDefinition<TForeignDocument, TAsElement> lookupPipeline,
  208. FieldDefinition<TNewResult, TAs> @as,
  209. AggregateLookupOptions<TForeignDocument, TNewResult> options = null)
  210. where TAs : IEnumerable<TAsElement>;
  211. /// <summary>
  212. /// Appends a match stage to the pipeline.
  213. /// </summary>
  214. /// <param name="filter">The filter.</param>
  215. /// <returns>The fluent aggregate interface.</returns>
  216. IAggregateFluent<TResult> Match(FilterDefinition<TResult> filter);
  217. /// <summary>
  218. /// Appends a match stage to the pipeline that matches derived documents and changes the result type to the derived type.
  219. /// </summary>
  220. /// <typeparam name="TNewResult">The type of the derived documents.</typeparam>
  221. /// <param name="newResultSerializer">The new result serializer.</param>
  222. /// <returns>The fluent aggregate interface.</returns>
  223. IAggregateFluent<TNewResult> OfType<TNewResult>(IBsonSerializer<TNewResult> newResultSerializer = null) where TNewResult : TResult;
  224. /// <summary>
  225. /// Appends an out stage to the pipeline and executes it, and then returns a cursor to read the contents of the output collection.
  226. /// </summary>
  227. /// <param name="collectionName">Name of the collection.</param>
  228. /// <param name="cancellationToken">The cancellation token.</param>
  229. /// <returns>A cursor.</returns>
  230. IAsyncCursor<TResult> Out(string collectionName, CancellationToken cancellationToken = default(CancellationToken));
  231. /// <summary>
  232. /// Appends an out stage to the pipeline and executes it, and then returns a cursor to read the contents of the output collection.
  233. /// </summary>
  234. /// <param name="collectionName">Name of the collection.</param>
  235. /// <param name="cancellationToken">The cancellation token.</param>
  236. /// <returns>A Task whose result is a cursor.</returns>
  237. Task<IAsyncCursor<TResult>> OutAsync(string collectionName, CancellationToken cancellationToken = default(CancellationToken));
  238. /// <summary>
  239. /// Appends a project stage to the pipeline.
  240. /// </summary>
  241. /// <typeparam name="TNewResult">The type of the result of the stage.</typeparam>
  242. /// <param name="projection">The projection.</param>
  243. /// <returns>
  244. /// The fluent aggregate interface.
  245. /// </returns>
  246. IAggregateFluent<TNewResult> Project<TNewResult>(ProjectionDefinition<TResult, TNewResult> projection);
  247. /// <summary>
  248. /// Appends a $replaceRoot stage to the pipeline.
  249. /// </summary>
  250. /// <typeparam name="TNewResult">The type of the new result.</typeparam>
  251. /// <param name="newRoot">The new root.</param>
  252. /// <returns>The fluent aggregate interface.</returns>
  253. IAggregateFluent<TNewResult> ReplaceRoot<TNewResult>(AggregateExpressionDefinition<TResult, TNewResult> newRoot);
  254. /// <summary>
  255. /// Appends a skip stage to the pipeline.
  256. /// </summary>
  257. /// <param name="skip">The number of documents to skip.</param>
  258. /// <returns>The fluent aggregate interface.</returns>
  259. IAggregateFluent<TResult> Skip(int skip);
  260. /// <summary>
  261. /// Appends a sort stage to the pipeline.
  262. /// </summary>
  263. /// <param name="sort">The sort specification.</param>
  264. /// <returns>The fluent aggregate interface.</returns>
  265. IAggregateFluent<TResult> Sort(SortDefinition<TResult> sort);
  266. /// <summary>
  267. /// Appends a sortByCount stage to the pipeline.
  268. /// </summary>
  269. /// <typeparam name="TId">The type of the identifier.</typeparam>
  270. /// <param name="id">The identifier.</param>
  271. /// <returns>The fluent aggregate interface.</returns>
  272. IAggregateFluent<AggregateSortByCountResult<TId>> SortByCount<TId>(AggregateExpressionDefinition<TResult, TId> id);
  273. /// <summary>
  274. /// Appends an unwind stage to the pipeline.
  275. /// </summary>
  276. /// <typeparam name="TNewResult">The type of the result of the stage.</typeparam>
  277. /// <param name="field">The field.</param>
  278. /// <param name="newResultSerializer">The new result serializer.</param>
  279. /// <returns>
  280. /// The fluent aggregate interface.
  281. /// </returns>
  282. [Obsolete("Use the Unwind overload which takes an options parameter.")]
  283. IAggregateFluent<TNewResult> Unwind<TNewResult>(FieldDefinition<TResult> field, IBsonSerializer<TNewResult> newResultSerializer);
  284. /// <summary>
  285. /// Appends an unwind stage to the pipeline.
  286. /// </summary>
  287. /// <typeparam name="TNewResult">The type of the new result.</typeparam>
  288. /// <param name="field">The field.</param>
  289. /// <param name="options">The options.</param>
  290. /// <returns>The fluent aggregate interface.</returns>
  291. IAggregateFluent<TNewResult> Unwind<TNewResult>(FieldDefinition<TResult> field, AggregateUnwindOptions<TNewResult> options = null);
  292. }
  293. /// <summary>
  294. /// Fluent interface for aggregate.
  295. /// </summary>
  296. /// <typeparam name="TResult">The type of the result.</typeparam>
  297. public interface IOrderedAggregateFluent<TResult> : IAggregateFluent<TResult>
  298. {
  299. /// <summary>
  300. /// Combines the current sort definition with an additional sort definition.
  301. /// </summary>
  302. /// <param name="newSort">The new sort.</param>
  303. /// <returns>The fluent aggregate interface.</returns>
  304. IOrderedAggregateFluent<TResult> ThenBy(SortDefinition<TResult> newSort);
  305. }
  306. }