Skip to content

Commit f139ddf

Browse files
committed
CSHARP-1671: fixes issue around groupings with a previous embedded pipeline.
1 parent 2c4caba commit f139ddf

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/MongoDB.Driver/Linq/Processors/CorrelatedGroupRewriter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ private Expression VisitCorrelatedGroup(CorrelatedExpression node)
6868
var groupExpression = (GroupByExpression)node.Expression;
6969
if (_accumulatorLookup != null && _accumulatorLookup.Contains(node.CorrelationId))
7070
{
71+
var oldAccumulatorLookup = _accumulatorLookup;
7172
var source = Visit(groupExpression.Source);
73+
_accumulatorLookup = oldAccumulatorLookup;
74+
7275
var accumulators = new List<AccumulatorExpression>();
7376
var fieldExpressions = new List<FieldExpression>();
7477
var comparer = new ExpressionComparer();

tests/MongoDB.Driver.Tests/Linq/MongoQueryableTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,26 @@ public async Task AverageAsync_with_selector()
110110
result.Should().Be(61);
111111
}
112112

113+
[Fact]
114+
public void GroupBy_combined_with_a_previous_embedded_pipeline()
115+
{
116+
var bs = new List<string>
117+
{
118+
"Baloon",
119+
"Balloon"
120+
};
121+
var query = CreateQuery()
122+
.Where(x => bs.Contains(x.B))
123+
.GroupBy(x => x.A)
124+
.Select(x => x.Max(y => y.C));
125+
126+
Assert(query,
127+
1,
128+
"{ $match: { 'B': { '$in': ['Baloon', 'Balloon'] } } }",
129+
"{ $group: { '_id': '$A', '__agg0': { '$max': '$C' } } }",
130+
"{ $project: { '__fld0': '$__agg0', '_id': 0 } }");
131+
}
132+
113133
[Fact]
114134
public void Count()
115135
{

0 commit comments

Comments
 (0)