Skip to content

Commit 805ccec

Browse files
committed
CSHARP-1820: Added pipeline and stage builders.
1 parent eaa5477 commit 805ccec

17 files changed

+2722
-791
lines changed

src/MongoDB.Driver/AggregateFacetOptions.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ namespace MongoDB.Driver
2020
/// <summary>
2121
/// Options for the aggregate $facet stage.
2222
/// </summary>
23-
/// <typeparam name="TNewResult">The type of the new result.</typeparam>
24-
public sealed class AggregateFacetOptions<TNewResult>
23+
/// <typeparam name="TOutput">The type of the output documents.</typeparam>
24+
public sealed class AggregateFacetOptions<TOutput>
2525
{
26-
private IBsonSerializer<TNewResult> _newResultSerializer;
26+
private IBsonSerializer<TOutput> _outputSerializer;
2727

2828
/// <summary>
29-
/// Gets or sets the new result serializer.
29+
/// Gets or sets the output serializer.
3030
/// </summary>
31-
public IBsonSerializer<TNewResult> NewResultSerializer
31+
public IBsonSerializer<TOutput> OutputSerializer
3232
{
33-
get { return _newResultSerializer; }
34-
set { _newResultSerializer = value; }
33+
get { return _outputSerializer; }
34+
set { _outputSerializer = value; }
3535
}
3636
}
3737
}

src/MongoDB.Driver/AggregateFluent.cs

Lines changed: 65 additions & 384 deletions
Large diffs are not rendered by default.

src/MongoDB.Driver/AggregateFluentBase.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ namespace MongoDB.Driver
2828
/// <typeparam name="TResult">The type of the document.</typeparam>
2929
public abstract class AggregateFluentBase<TResult> : IOrderedAggregateFluent<TResult>
3030
{
31+
/// <inheritdoc />
32+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations")]
33+
public virtual IMongoDatabase Database
34+
{
35+
get { throw new NotImplementedException(); }
36+
}
37+
3138
/// <inheritdoc />
3239
public abstract AggregateOptions Options { get; }
3340

@@ -93,15 +100,15 @@ public virtual IAggregateFluent<TNewResult> Facet<TNewResult>(
93100
}
94101

95102
/// <inheritdoc />
96-
public virtual IAggregateFluent<TNewResult> GraphLookup<TNewResult, TFrom, TConnect, TConnectFrom, TStartWith, TAs, TAsEnumerable>(
103+
public virtual IAggregateFluent<TNewResult> GraphLookup<TFrom, TConnectFrom, TConnectTo, TStartWith, TAsElement, TAs, TNewResult>(
97104
IMongoCollection<TFrom> from,
98105
FieldDefinition<TFrom, TConnectFrom> connectFromField,
99-
FieldDefinition<TFrom, TConnect> connectToField,
106+
FieldDefinition<TFrom, TConnectTo> connectToField,
100107
AggregateExpressionDefinition<TResult, TStartWith> startWith,
101-
FieldDefinition<TNewResult, TAsEnumerable> @as,
102-
FieldDefinition<TAs, int> depthField,
103-
AggregateGraphLookupOptions<TNewResult, TFrom, TConnect, TConnectFrom, TStartWith, TAs, TAsEnumerable> options = null)
104-
where TAsEnumerable : IEnumerable<TAs>
108+
FieldDefinition<TNewResult, TAs> @as,
109+
FieldDefinition<TAsElement, int> depthField,
110+
AggregateGraphLookupOptions<TFrom, TAsElement, TNewResult> options = null)
111+
where TAs : IEnumerable<TAsElement>
105112
{
106113
throw new NotImplementedException();
107114
}
@@ -154,6 +161,12 @@ public virtual IAggregateFluent<AggregateSortByCountResult<TId>> SortByCount<TId
154161
throw new NotImplementedException();
155162
}
156163

164+
/// <inheritdoc />
165+
public virtual IOrderedAggregateFluent<TResult> ThenBy(SortDefinition<TResult> newSort)
166+
{
167+
throw new NotImplementedException();
168+
}
169+
157170
/// <inheritdoc />
158171
public abstract IAggregateFluent<TNewResult> Unwind<TNewResult>(FieldDefinition<TResult> field, IBsonSerializer<TNewResult> newResultSerializer);
159172

src/MongoDB.Driver/AggregateGraphLookupOptions.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,15 @@ namespace MongoDB.Driver
2121
/// <summary>
2222
/// Represents options for the GraphLookup method.
2323
/// </summary>
24-
public class AggregateGraphLookupOptions<TNewResult, TFrom, TConnect, TConnectFrom, TStartWith, TAs, TAsEnumerable>
25-
where TAsEnumerable : IEnumerable<TAs>
24+
/// <typeparam name="TFrom">The type of from documents.</typeparam>
25+
/// <typeparam name="TAsElement">The type of the as field elements.</typeparam>
26+
/// <typeparam name="TOutput">The type of the output documents.</typeparam>
27+
public class AggregateGraphLookupOptions<TFrom, TAsElement, TOutput>
2628
{
27-
internal AggregateGraphLookupOptions()
28-
{
29-
}
30-
3129
/// <summary>
32-
/// Gets or sets the TAs serialzier.
30+
/// Gets or sets the TAsElement serialzier.
3331
/// </summary>
34-
public IBsonSerializer<TAs> AsSerializer { get; set; }
32+
public IBsonSerializer<TAsElement> AsElementSerializer { get; set; }
3533

3634
/// <summary>
3735
/// Gets or sets the TFrom serializer.
@@ -44,9 +42,9 @@ internal AggregateGraphLookupOptions()
4442
public int? MaxDepth { get; set; }
4543

4644
/// <summary>
47-
/// Gets or sets the TNewResult serializer.
45+
/// Gets or sets the output serializer.
4846
/// </summary>
49-
public IBsonSerializer<TNewResult> NewResultSerializer { get; set; }
47+
public IBsonSerializer<TOutput> OutputSerializer { get; set; }
5048

5149
/// <summary>
5250
/// Gets the filter to restrict the search with.

src/MongoDB.Driver/FilteredMongoCollectionBase.cs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -234,22 +234,9 @@ private IEnumerable<WriteModel<TDocument>> CombineModelFilters(IEnumerable<Write
234234

235235
private PipelineDefinition<TDocument, TResult> CreateFilteredPipeline<TResult>(PipelineDefinition<TDocument, TResult> pipeline)
236236
{
237-
const string matchOperatorName = "$match";
238-
239-
var filterStage = new DelegatedPipelineStageDefinition<TDocument, TDocument>(
240-
matchOperatorName,
241-
(s, sr) =>
242-
{
243-
var renderedFilter = _filter.Render(s, sr);
244-
return new RenderedPipelineStageDefinition<TDocument>(matchOperatorName, new BsonDocument(matchOperatorName, renderedFilter), s);
245-
});
246-
247-
var filterPipeline = new PipelineStagePipelineDefinition<TDocument, TDocument>(new[] { filterStage });
248-
var combinedPipeline = new CombinedPipelineDefinition<TDocument, TDocument, TResult>(
249-
filterPipeline,
250-
pipeline);
251-
252-
return new OptimizingPipelineDefinition<TDocument, TResult>(combinedPipeline);
237+
var filterStage = PipelineStageDefinitionBuilder.Match(_filter);
238+
var filteredPipeline = new PrependedStagePipelineDefinition<TDocument, TDocument, TResult>(filterStage, pipeline);
239+
return new OptimizingPipelineDefinition<TDocument, TResult>(filteredPipeline);
253240
}
254241
}
255242
}

src/MongoDB.Driver/IAggregateFluent.cs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ namespace MongoDB.Driver
3232
/// <typeparam name="TResult">The type of the result of the pipeline.</typeparam>
3333
public interface IAggregateFluent<TResult> : IAsyncCursorSource<TResult>
3434
{
35+
/// <summary>
36+
/// Gets the database.
37+
/// </summary>
38+
IMongoDatabase Database { get; }
39+
3540
/// <summary>
3641
/// Gets the options.
3742
/// </summary>
@@ -51,7 +56,7 @@ public interface IAggregateFluent<TResult> : IAsyncCursorSource<TResult>
5156
IAggregateFluent<TNewResult> AppendStage<TNewResult>(PipelineStageDefinition<TResult, TNewResult> stage);
5257

5358
/// <summary>
54-
/// Appends a project stage to the pipeline.
59+
/// Changes the result type of the pipeline.
5560
/// </summary>
5661
/// <typeparam name="TNewResult">The type of the new result.</typeparam>
5762
/// <param name="newResultSerializer">The new result serializer.</param>
@@ -138,13 +143,13 @@ IAggregateFluent<TNewResult> Facet<TNewResult>(
138143
/// <summary>
139144
/// Appends a $graphLookup stage to the pipeline.
140145
/// </summary>
141-
/// <typeparam name="TNewResult">The type of the new result (must be same as TResult with an additional as field).</typeparam>
142146
/// <typeparam name="TFrom">The type of the from documents.</typeparam>
143-
/// <typeparam name="TConnect">The type of the connect field.</typeparam>
144-
/// <typeparam name="TConnectFrom">The type of the connect from field (must be either TConnect or a type that implements IEnumerable{TConnect}).</typeparam>
145-
/// <typeparam name="TStartWith">The type of the start with expression (must be either TConnect or a type that implements IEnumerable{TConnect}).</typeparam>
146-
/// <typeparam name="TAs">The type of the documents in the as field.</typeparam>
147-
/// <typeparam name="TAsEnumerable">The type of the enumerable as field.</typeparam>
147+
/// <typeparam name="TConnectFrom">The type of the connect from field (must be either TConnectTo or a type that implements IEnumerable{TConnectTo}).</typeparam>
148+
/// <typeparam name="TConnectTo">The type of the connect to field.</typeparam>
149+
/// <typeparam name="TStartWith">The type of the start with expression (must be either TConnectTo or a type that implements IEnumerable{TConnectTo}).</typeparam>
150+
/// <typeparam name="TAsElement">The type of the as field elements.</typeparam>
151+
/// <typeparam name="TAs">The type of the as field.</typeparam>
152+
/// <typeparam name="TNewResult">The type of the new result (must be same as TResult with an additional as field).</typeparam>
148153
/// <param name="from">The from collection.</param>
149154
/// <param name="connectFromField">The connect from field.</param>
150155
/// <param name="connectToField">The connect to field.</param>
@@ -153,15 +158,15 @@ IAggregateFluent<TNewResult> Facet<TNewResult>(
153158
/// <param name="depthField">The depth field.</param>
154159
/// <param name="options">The options.</param>
155160
/// <returns>The fluent aggregate interface.</returns>
156-
IAggregateFluent<TNewResult> GraphLookup<TNewResult, TFrom, TConnect, TConnectFrom, TStartWith, TAs, TAsEnumerable>(
161+
IAggregateFluent<TNewResult> GraphLookup<TFrom, TConnectFrom, TConnectTo, TStartWith, TAsElement, TAs, TNewResult>(
157162
IMongoCollection<TFrom> from,
158163
FieldDefinition<TFrom, TConnectFrom> connectFromField,
159-
FieldDefinition<TFrom, TConnect> connectToField,
164+
FieldDefinition<TFrom, TConnectTo> connectToField,
160165
AggregateExpressionDefinition<TResult, TStartWith> startWith,
161-
FieldDefinition<TNewResult, TAsEnumerable> @as,
162-
FieldDefinition<TAs, int> depthField,
163-
AggregateGraphLookupOptions<TNewResult, TFrom, TConnect, TConnectFrom, TStartWith, TAs, TAsEnumerable> options = null)
164-
where TAsEnumerable : IEnumerable<TAs>;
166+
FieldDefinition<TNewResult, TAs> @as,
167+
FieldDefinition<TAsElement, int> depthField,
168+
AggregateGraphLookupOptions<TFrom, TAsElement, TNewResult> options = null)
169+
where TAs : IEnumerable<TAsElement>;
165170

166171
/// <summary>
167172
/// Appends a group stage to the pipeline.
@@ -290,5 +295,11 @@ IAggregateFluent<TNewResult> GraphLookup<TNewResult, TFrom, TConnect, TConnectFr
290295
/// <typeparam name="TResult">The type of the result.</typeparam>
291296
public interface IOrderedAggregateFluent<TResult> : IAggregateFluent<TResult>
292297
{
298+
/// <summary>
299+
/// Combines the current sort definition with an additional sort definition.
300+
/// </summary>
301+
/// <param name="newSort">The new sort.</param>
302+
/// The fluent aggregate interface.
303+
IOrderedAggregateFluent<TResult> ThenBy(SortDefinition<TResult> newSort);
293304
}
294305
}

0 commit comments

Comments
 (0)