Skip to content

Commit f52cec5

Browse files
committed
changed fluent find to be more like aggregate.
1 parent 2b81a59 commit f52cec5

10 files changed

+126
-170
lines changed

src/MongoDB.Driver.Tests/IAggregateFluentExtensionsTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,16 +195,16 @@ public void Unwind_with_expression_to_new_result_should_generate_the_correct_unw
195195
var expectedUnwind = BsonDocument.Parse("{$unwind: '$Age'}");
196196

197197
Assert.AreEqual(expectedUnwind, subject.Pipeline.Last());
198-
Assert.AreSame(BsonDocumentSerializer.Instance, subject.ResultSerializer);
198+
Assert.AreSame(BsonDocumentSerializer.Instance, subject.Options.ResultSerializer);
199199
}
200200

201201
private IAggregateFluent<Person, Person> CreateSubject()
202202
{
203203
var settings = new MongoCollectionSettings();
204204
var collection = Substitute.For<IMongoCollection<Person>>();
205205
collection.Settings.Returns(settings);
206-
var options = new AggregateOptions();
207-
var subject = new AggregateFluent<Person, Person>(collection, new List<object>(), options, settings.SerializerRegistry.GetSerializer<Person>());
206+
var options = new AggregateOptions<Person>();
207+
var subject = new AggregateFluent<Person, Person>(collection, new List<object>(), options);
208208

209209
return subject;
210210
}

src/MongoDB.Driver.Tests/MongoCollectionImplTests.cs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -585,19 +585,21 @@ public async Task Find_with_an_expression_should_execute_correctly()
585585
Expression<Func<BsonDocument, bool>> filter = doc => doc["x"] == 1;
586586
var projection = BsonDocument.Parse("{y:1}");
587587
var sort = BsonDocument.Parse("{a:1}");
588-
var fluent = _subject.Find(filter)
588+
var options = new FindOptions
589+
{
590+
AllowPartialResults = true,
591+
BatchSize = 20,
592+
Comment = "funny",
593+
CursorType = CursorType.TailableAwait,
594+
MaxTime = TimeSpan.FromSeconds(3),
595+
Modifiers = BsonDocument.Parse("{$snapshot: true}"),
596+
NoCursorTimeout = true
597+
};
598+
var fluent = _subject.Find(filter, options)
589599
.Projection<BsonDocument>(projection)
590600
.Sort(sort)
591-
.AllowPartialResults(true)
592-
.BatchSize(20)
593-
.Comment("funny")
594-
.CursorType(CursorType.TailableAwait)
595601
.Limit(30)
596-
.MaxTime(TimeSpan.FromSeconds(3))
597-
.Modifiers(BsonDocument.Parse("{$snapshot: true}"))
598-
.NoCursorTimeout(true)
599602
.Skip(40);
600-
var options = fluent.Options;
601603

602604
var fakeCursor = Substitute.For<IAsyncCursor<BsonDocument>>();
603605
_operationExecutor.EnqueueResult(fakeCursor);
@@ -614,12 +616,12 @@ public async Task Find_with_an_expression_should_execute_correctly()
614616
operation.Comment.Should().Be("funny");
615617
operation.CursorType.Should().Be(MongoDB.Driver.Core.Operations.CursorType.TailableAwait);
616618
operation.Filter.Should().Be(BsonDocument.Parse("{x:1}"));
617-
operation.Limit.Should().Be(options.Limit);
619+
operation.Limit.Should().Be(30);
618620
operation.MaxTime.Should().Be(options.MaxTime);
619621
operation.Modifiers.Should().Be(options.Modifiers);
620622
operation.NoCursorTimeout.Should().Be(options.NoCursorTimeout);
621623
operation.Projection.Should().Be(projection);
622-
operation.Skip.Should().Be(options.Skip);
624+
operation.Skip.Should().Be(40);
623625
operation.Sort.Should().Be(sort);
624626
}
625627

@@ -629,19 +631,21 @@ public async Task Find_should_execute_the_FindOperation()
629631
var filter = BsonDocument.Parse("{x:1}");
630632
var projection = BsonDocument.Parse("{y:1}");
631633
var sort = BsonDocument.Parse("{a:1}");
632-
var fluent = _subject.Find(filter)
634+
var options = new FindOptions
635+
{
636+
AllowPartialResults = true,
637+
BatchSize = 20,
638+
Comment = "funny",
639+
CursorType = CursorType.TailableAwait,
640+
MaxTime = TimeSpan.FromSeconds(3),
641+
Modifiers = BsonDocument.Parse("{$snapshot: true}"),
642+
NoCursorTimeout = true
643+
};
644+
var fluent = _subject.Find(filter, options)
633645
.Projection<BsonDocument>(projection)
634646
.Sort(sort)
635-
.AllowPartialResults(true)
636-
.BatchSize(20)
637-
.Comment("funny")
638-
.CursorType(CursorType.TailableAwait)
639647
.Limit(30)
640-
.MaxTime(TimeSpan.FromSeconds(3))
641-
.Modifiers(BsonDocument.Parse("{$snapshot: true}"))
642-
.NoCursorTimeout(true)
643648
.Skip(40);
644-
var options = fluent.Options;
645649

646650
var fakeCursor = Substitute.For<IAsyncCursor<BsonDocument>>();
647651
_operationExecutor.EnqueueResult(fakeCursor);
@@ -658,12 +662,12 @@ public async Task Find_should_execute_the_FindOperation()
658662
operation.Comment.Should().Be("funny");
659663
operation.CursorType.Should().Be(MongoDB.Driver.Core.Operations.CursorType.TailableAwait);
660664
operation.Filter.Should().Be(filter);
661-
operation.Limit.Should().Be(options.Limit);
665+
operation.Limit.Should().Be(30);
662666
operation.MaxTime.Should().Be(options.MaxTime);
663667
operation.Modifiers.Should().Be(options.Modifiers);
664668
operation.NoCursorTimeout.Should().Be(options.NoCursorTimeout);
665669
operation.Projection.Should().Be(projection);
666-
operation.Skip.Should().Be(options.Skip);
670+
operation.Skip.Should().Be(40);
667671
operation.Sort.Should().Be(sort);
668672
}
669673

src/MongoDB.Driver/AggregateFluent.cs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,15 @@ internal class AggregateFluent<TDocument, TResult> : IOrderedAggregateFluent<TDo
2727
{
2828
// fields
2929
private readonly IMongoCollection<TDocument> _collection;
30-
private readonly AggregateOptions _options;
30+
private readonly AggregateOptions<TResult> _options;
3131
private readonly IList<object> _pipeline;
32-
private readonly IBsonSerializer<TResult> _resultSerializer;
3332

3433
// constructors
35-
public AggregateFluent(IMongoCollection<TDocument> collection, IEnumerable<object> pipeline, AggregateOptions options, IBsonSerializer<TResult> resultSerializer)
34+
public AggregateFluent(IMongoCollection<TDocument> collection, IEnumerable<object> pipeline, AggregateOptions<TResult> options)
3635
{
3736
_collection = Ensure.IsNotNull(collection, "collection");
3837
_pipeline = Ensure.IsNotNull(pipeline, "pipeline").ToList();
3938
_options = Ensure.IsNotNull(options, "options");
40-
_resultSerializer = resultSerializer;
4139
}
4240

4341
// properties
@@ -46,7 +44,7 @@ public IMongoCollection<TDocument> Collection
4644
get { return _collection; }
4745
}
4846

49-
public AggregateOptions Options
47+
public AggregateOptions<TResult> Options
5048
{
5149
get { return _options; }
5250
}
@@ -56,11 +54,6 @@ public IList<object> Pipeline
5654
get { return _pipeline; }
5755
}
5856

59-
public IBsonSerializer<TResult> ResultSerializer
60-
{
61-
get { return _resultSerializer; }
62-
}
63-
6457
// methods
6558
public IAggregateFluent<TDocument, TResult> AppendStage(object stage)
6659
{
@@ -141,20 +134,20 @@ public IAggregateFluent<TDocument, TNewResult> Unwind<TNewResult>(string fieldNa
141134

142135
public Task<IAsyncCursor<TResult>> ToCursorAsync(CancellationToken cancellationToken = default(CancellationToken))
143136
{
144-
var options = new AggregateOptions<TResult>
137+
return _collection.AggregateAsync(_pipeline, _options, cancellationToken);
138+
}
139+
140+
private IAggregateFluent<TDocument, TNewResult> CloneWithNewResultType<TNewResult>(IBsonSerializer<TNewResult> resultSerializer)
141+
{
142+
var newOptions = new AggregateOptions<TNewResult>
145143
{
146144
AllowDiskUse = _options.AllowDiskUse,
147145
BatchSize = _options.BatchSize,
148146
MaxTime = _options.MaxTime,
149-
ResultSerializer = _resultSerializer,
147+
ResultSerializer = resultSerializer,
150148
UseCursor = _options.UseCursor
151149
};
152-
return _collection.AggregateAsync(_pipeline, options, cancellationToken);
153-
}
154-
155-
private IAggregateFluent<TDocument, TNewResult> CloneWithNewResultType<TNewResult>(IBsonSerializer<TNewResult> resultSerializer)
156-
{
157-
return new AggregateFluent<TDocument, TNewResult>(_collection, _pipeline, _options, resultSerializer);
150+
return new AggregateFluent<TDocument, TNewResult>(_collection, _pipeline, newOptions);
158151
}
159152

160153
private BsonDocument ConvertToBsonDocument(object document)

src/MongoDB.Driver/FindFluent.cs

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,6 @@ public FindOptions<TResult> Options
4040
}
4141

4242
// methods
43-
public IFindFluent<TDocument, TResult> AllowPartialResults(bool allowPartialResults)
44-
{
45-
_options.AllowPartialResults = allowPartialResults;
46-
return this;
47-
}
48-
49-
public IFindFluent<TDocument, TResult> BatchSize(int? size)
50-
{
51-
_options.BatchSize = size;
52-
return this;
53-
}
54-
55-
public IFindFluent<TDocument, TResult> Comment(string comment)
56-
{
57-
_options.Comment = comment;
58-
return this;
59-
}
60-
6143
public Task<long> CountAsync(CancellationToken cancellationToken)
6244
{
6345
BsonValue hint;
@@ -73,36 +55,12 @@ public Task<long> CountAsync(CancellationToken cancellationToken)
7355
return _collection.CountAsync(_filter, options, cancellationToken);
7456
}
7557

76-
public IFindFluent<TDocument, TResult> CursorType(CursorType cursorType)
77-
{
78-
_options.CursorType = cursorType;
79-
return this;
80-
}
81-
8258
public IFindFluent<TDocument, TResult> Limit(int? limit)
8359
{
8460
_options.Limit = limit;
8561
return this;
8662
}
8763

88-
public IFindFluent<TDocument, TResult> MaxTime(TimeSpan? maxTime)
89-
{
90-
_options.MaxTime = maxTime;
91-
return this;
92-
}
93-
94-
public IFindFluent<TDocument, TResult> Modifiers(BsonDocument modifiers)
95-
{
96-
_options.Modifiers = modifiers;
97-
return this;
98-
}
99-
100-
public IFindFluent<TDocument, TResult> NoCursorTimeout(bool noCursorTimeout)
101-
{
102-
_options.NoCursorTimeout = noCursorTimeout;
103-
return this;
104-
}
105-
10664
public IFindFluent<TDocument, TNewResult> Projection<TNewResult>(object projection)
10765
{
10866
return Projection<TNewResult>(projection, null);

src/MongoDB.Driver/FindOptions.cs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,24 @@
99
namespace MongoDB.Driver
1010
{
1111
/// <summary>
12-
/// Options for finding documents.
12+
/// Options for a find operation.
1313
/// </summary>
14-
/// <typeparam name="TResult">The type of the result.</typeparam>
15-
public class FindOptions<TResult>
14+
public abstract class FindOptionsBase
1615
{
1716
// fields
1817
private bool _allowPartialResults;
1918
private int? _batchSize;
2019
private string _comment;
2120
private CursorType _cursorType;
22-
private int? _limit;
2321
private TimeSpan? _maxTime;
2422
private BsonDocument _modifiers;
2523
private bool _noCursorTimeout;
26-
private object _projection;
27-
private IBsonSerializer<TResult> _resultSerializer;
28-
private int? _skip;
29-
private object _sort;
3024

3125
// constructors
3226
/// <summary>
33-
/// Initializes a new instance of the <see cref="FindOptions{TResult}"/> class.
27+
/// Initializes a new instance of the <see cref="FindOptionsBase"/> class.
3428
/// </summary>
35-
public FindOptions()
29+
public FindOptionsBase()
3630
{
3731
_cursorType = CursorType.NonTailable;
3832
}
@@ -74,15 +68,6 @@ public CursorType CursorType
7468
set { _cursorType = value; }
7569
}
7670

77-
/// <summary>
78-
/// Gets or sets how many documents to return.
79-
/// </summary>
80-
public int? Limit
81-
{
82-
get { return _limit; }
83-
set { _limit = value; }
84-
}
85-
8671
/// <summary>
8772
/// Gets or sets the maximum time.
8873
/// </summary>
@@ -102,13 +87,43 @@ public BsonDocument Modifiers
10287
}
10388

10489
/// <summary>
105-
/// Gets or sets a value indicating whether the cursor should not timeout.
90+
/// Gets or sets whether a cursor will time out.
10691
/// </summary>
10792
public bool NoCursorTimeout
10893
{
10994
get { return _noCursorTimeout; }
11095
set { _noCursorTimeout = value; }
11196
}
97+
}
98+
99+
/// <summary>
100+
/// Options for finding documents.
101+
/// </summary>
102+
public class FindOptions : FindOptionsBase
103+
{ }
104+
105+
/// <summary>
106+
/// Options for finding documents.
107+
/// </summary>
108+
/// <typeparam name="TResult">The type of the result.</typeparam>
109+
public class FindOptions<TResult> : FindOptionsBase
110+
{
111+
// fields
112+
private int? _limit;
113+
private object _projection;
114+
private IBsonSerializer<TResult> _resultSerializer;
115+
private int? _skip;
116+
private object _sort;
117+
118+
// properties
119+
/// <summary>
120+
/// Gets or sets how many documents to return.
121+
/// </summary>
122+
public int? Limit
123+
{
124+
get { return _limit; }
125+
set { _limit = value; }
126+
}
112127

113128
/// <summary>
114129
/// Gets or sets the projection.

src/MongoDB.Driver/IAggregateFluent.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,13 @@ public interface IAggregateFluent<TDocument, TResult> : IAsyncCursorSource<TResu
3535
/// <summary>
3636
/// Gets the options.
3737
/// </summary>
38-
AggregateOptions Options { get; }
38+
AggregateOptions<TResult> Options { get; }
3939

4040
/// <summary>
4141
/// Gets the pipeline.
4242
/// </summary>
4343
IList<object> Pipeline { get; }
4444

45-
/// <summary>
46-
/// Gets the result serializer.
47-
/// </summary>
48-
IBsonSerializer<TResult> ResultSerializer { get; }
49-
5045
/// <summary>
5146
/// Appends a stage to the pipeline.
5247
/// </summary>

src/MongoDB.Driver/IAggregateFluentExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static IAggregateFluent<TDocument, TNewResult> Group<TDocument, TResult,
6767
Ensure.IsNotNull(idProjector, "idProjector");
6868
Ensure.IsNotNull(groupProjector, "groupProjector");
6969

70-
var serializer = source.ResultSerializer ?? source.Collection.Settings.SerializerRegistry.GetSerializer<TResult>();
70+
var serializer = source.Options.ResultSerializer ?? source.Collection.Settings.SerializerRegistry.GetSerializer<TResult>();
7171
var projectionInfo = AggregateProjectionTranslator.TranslateGroup<TKey, TResult, TNewResult>(idProjector, groupProjector, serializer, source.Collection.Settings.SerializerRegistry);
7272

7373
return source.Group<TNewResult>(projectionInfo.Projection, projectionInfo.Serializer);
@@ -119,7 +119,7 @@ public static IAggregateFluent<TDocument, TNewResult> Project<TDocument, TResult
119119
Ensure.IsNotNull(source, "source");
120120
Ensure.IsNotNull(project, "projector");
121121

122-
var serializer = source.ResultSerializer ?? source.Collection.Settings.SerializerRegistry.GetSerializer<TResult>();
122+
var serializer = source.Options.ResultSerializer ?? source.Collection.Settings.SerializerRegistry.GetSerializer<TResult>();
123123
var projectionInfo = AggregateProjectionTranslator.TranslateProject(project, serializer, source.Collection.Settings.SerializerRegistry);
124124

125125
return source.Project<TNewResult>(projectionInfo.Projection, projectionInfo.Serializer);
@@ -144,7 +144,7 @@ public static IOrderedAggregateFluent<TDocument, TResult> SortBy<TDocument, TRes
144144

145145
source = source.Sort(sortDocument);
146146

147-
return new AggregateFluent<TDocument, TResult>(source.Collection, source.Pipeline, source.Options, source.ResultSerializer);
147+
return new AggregateFluent<TDocument, TResult>(source.Collection, source.Pipeline, source.Options);
148148
}
149149

150150
/// <summary>
@@ -166,7 +166,7 @@ public static IOrderedAggregateFluent<TDocument, TResult> SortByDescending<TDocu
166166

167167
source = source.Sort(sortDocument);
168168

169-
return new AggregateFluent<TDocument, TResult>(source.Collection, source.Pipeline, source.Options, source.ResultSerializer);
169+
return new AggregateFluent<TDocument, TResult>(source.Collection, source.Pipeline, source.Options);
170170
}
171171

172172
/// <summary>

0 commit comments

Comments
 (0)