Skip to content

Commit 74cd796

Browse files
committed
CSHARP-1743: Add support for $count stage to IAggregateFluent.
1 parent d410f5d commit 74cd796

File tree

7 files changed

+163
-4
lines changed

7 files changed

+163
-4
lines changed

src/MongoDB.Driver.Core/Core/Misc/Feature.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class Feature
2525
#region static
2626
private static readonly Feature __aggregate = new Feature("Aggregate", new SemanticVersion(2, 2, 0));
2727
private static readonly Feature __aggregateAllowDiskUse = new Feature("AggregateAllowDiskUse", new SemanticVersion(2, 6, 0));
28+
private static readonly Feature __aggregateCountStage = new Feature("AggregateCountStage", new SemanticVersion(3, 3, 11));
2829
private static readonly Feature __aggregateCursorResult = new Feature("AggregateCursorResult", new SemanticVersion(2, 6, 0));
2930
private static readonly Feature __aggregateExplain = new Feature("AggregateExplain", new SemanticVersion(2, 6, 0));
3031
private static readonly Feature __aggregateOut = new Feature("Aggregate", new SemanticVersion(2, 6, 0));
@@ -60,6 +61,11 @@ public class Feature
6061
/// </summary>
6162
public static Feature AggregateAllowDiskUse => __aggregateAllowDiskUse;
6263

64+
/// <summary>
65+
/// Gets the aggregate count stage feature.
66+
/// </summary>
67+
public static Feature AggregateCountStage => __aggregateCountStage;
68+
6369
/// <summary>
6470
/// Gets the aggregate cursor result feature.
6571
/// </summary>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* Copyright 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+
16+
using MongoDB.Bson.Serialization;
17+
using MongoDB.Bson.Serialization.Attributes;
18+
19+
namespace MongoDB.Driver
20+
{
21+
/// <summary>
22+
/// Default result type for the aggregate $count stage.
23+
/// </summary>
24+
public sealed class AggregateCountResult
25+
{
26+
private long _count;
27+
28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="AggregateCountResult"/> class.
30+
/// </summary>
31+
/// <param name="count">The count.</param>
32+
public AggregateCountResult(long count)
33+
{
34+
_count = count;
35+
}
36+
37+
/// <summary>
38+
/// Gets the count.
39+
/// </summary>
40+
/// <value>
41+
/// The count.
42+
/// </value>
43+
[BsonElement("count")]
44+
public long Count
45+
{
46+
get { return _count; }
47+
}
48+
}
49+
}

src/MongoDB.Driver/AggregateFluent.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2015 MongoDB Inc.
1+
/* Copyright 2010-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -67,6 +67,22 @@ public override IAggregateFluent<TNewResult> As<TNewResult>(IBsonSerializer<TNew
6767
return Project(projection);
6868
}
6969

70+
public override IAggregateFluent<AggregateCountResult> Count()
71+
{
72+
const string operatorName = "$count";
73+
var stage = new DelegatedPipelineStageDefinition<TResult, AggregateCountResult>(
74+
operatorName,
75+
(s, sr) =>
76+
{
77+
return new RenderedPipelineStageDefinition<AggregateCountResult>(
78+
operatorName,
79+
new BsonDocument(operatorName, "count"),
80+
sr.GetSerializer<AggregateCountResult>());
81+
});
82+
83+
return AppendStage<AggregateCountResult>(stage);
84+
}
85+
7086
public override IAggregateFluent<TNewResult> Group<TNewResult>(ProjectionDefinition<TResult, TNewResult> group)
7187
{
7288
const string operatorName = "$group";

src/MongoDB.Driver/AggregateFluentBase.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2015 MongoDB Inc.
1+
/* Copyright 2010-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -40,6 +40,12 @@ public abstract class AggregateFluentBase<TResult> : IOrderedAggregateFluent<TRe
4040
/// <inheritdoc />
4141
public abstract IAggregateFluent<TNewResult> As<TNewResult>(IBsonSerializer<TNewResult> newResultSerializer);
4242

43+
/// <inheritdoc />
44+
public virtual IAggregateFluent<AggregateCountResult> Count()
45+
{
46+
throw new NotImplementedException();
47+
}
48+
4349
/// <inheritdoc />
4450
public abstract IAggregateFluent<TNewResult> Group<TNewResult>(ProjectionDefinition<TResult, TNewResult> group);
4551

src/MongoDB.Driver/IAggregateFluent.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2015 MongoDB Inc.
1+
/* Copyright 2010-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -58,6 +58,12 @@ public interface IAggregateFluent<TResult> : IAsyncCursorSource<TResult>
5858
/// <returns>The fluent aggregate interface.</returns>
5959
IAggregateFluent<TNewResult> As<TNewResult>(IBsonSerializer<TNewResult> newResultSerializer = null);
6060

61+
/// <summary>
62+
/// Appends a count stage to the pipeline.
63+
/// </summary>
64+
/// <returns>The fluent aggregate interface.</returns>
65+
IAggregateFluent<AggregateCountResult> Count();
66+
6167
/// <summary>
6268
/// Appends a group stage to the pipeline.
6369
/// </summary>

src/MongoDB.Driver/MongoDB.Driver.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<Compile Include="..\MongoDB.Shared\Hasher.cs">
7878
<Link>Support\Hasher.cs</Link>
7979
</Compile>
80+
<Compile Include="AggregateCountResult.cs" />
8081
<Compile Include="AggregateFluent.cs" />
8182
<Compile Include="AggregateLookupOptions.cs" />
8283
<Compile Include="AggregateStringTranslationMode.cs" />

tests/MongoDB.Driver.Tests/AggregateFluentTests.cs

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
using System;
1717
using System.Linq;
1818
using System.Threading;
19+
using FluentAssertions;
1920
using MongoDB.Bson;
2021
using MongoDB.Bson.Serialization;
2122
using MongoDB.Bson.Serialization.Serializers;
2223
using MongoDB.Bson.TestHelpers.XunitExtensions;
24+
using MongoDB.Driver.Core.Misc;
25+
using MongoDB.Driver.Core.TestHelpers.XunitExtensions;
2326
using Moq;
2427
using Xunit;
2528

@@ -70,7 +73,79 @@ public void As_should_add_the_expected_stage(
7073
It.IsAny<AggregateOptions>(),
7174
CancellationToken.None),
7275
Times.Once);
73-
}
76+
}
77+
}
78+
79+
[Theory]
80+
[ParameterAttributeData]
81+
public void Count_should_add_the_expected_stage(
82+
[Values(false, true)]
83+
bool async)
84+
{
85+
var subject = CreateSubject();
86+
87+
var result = subject.Count();
88+
89+
Predicate<PipelineDefinition<C, AggregateCountResult>> isExpectedPipeline = pipeline =>
90+
{
91+
var renderedPipeline = RenderPipeline(pipeline);
92+
return
93+
renderedPipeline.Documents.Count == 1 &&
94+
renderedPipeline.Documents[0] == BsonDocument.Parse("{ $count : 'count' }") &&
95+
renderedPipeline.OutputSerializer.ValueType == typeof(AggregateCountResult);
96+
};
97+
98+
if (async)
99+
{
100+
result.ToCursorAsync().GetAwaiter().GetResult();
101+
102+
_mockCollection.Verify(
103+
c => c.AggregateAsync<AggregateCountResult>(
104+
It.Is<PipelineDefinition<C, AggregateCountResult>>(pipeline => isExpectedPipeline(pipeline)),
105+
It.IsAny<AggregateOptions>(),
106+
CancellationToken.None),
107+
Times.Once);
108+
}
109+
else
110+
{
111+
result.ToCursor();
112+
113+
_mockCollection.Verify(
114+
c => c.Aggregate<AggregateCountResult>(
115+
It.Is<PipelineDefinition<C, AggregateCountResult>>(pipeline => isExpectedPipeline(pipeline)),
116+
It.IsAny<AggregateOptions>(),
117+
CancellationToken.None),
118+
Times.Once);
119+
}
120+
}
121+
122+
[SkippableTheory]
123+
[ParameterAttributeData]
124+
public void Count_should_return_the_expected_result(
125+
[Values(false, true)]
126+
bool async)
127+
{
128+
RequireServer.Check().Supports(Feature.AggregateCountStage);
129+
var client = DriverTestConfiguration.Client;
130+
var databaseNamespace = CoreTestConfiguration.DatabaseNamespace;
131+
var collectionNamespace = CoreTestConfiguration.GetCollectionNamespaceForTestMethod();
132+
var database = client.GetDatabase(databaseNamespace.DatabaseName);
133+
database.DropCollection(collectionNamespace.CollectionName);
134+
var collection = database.GetCollection<BsonDocument>(collectionNamespace.CollectionName);
135+
collection.InsertOne(new BsonDocument());
136+
var subject = collection.Aggregate();
137+
138+
long result;
139+
if (async)
140+
{
141+
result = subject.Count().SingleAsync().GetAwaiter().GetResult().Count;
142+
}
143+
else
144+
{
145+
result = subject.Count().Single().Count;
146+
}
147+
148+
result.Should().Be(1);
74149
}
75150

76151
[Theory]

0 commit comments

Comments
 (0)