Skip to content

Commit e446cc9

Browse files
committed
CSHARP-1702: adding support for field projection into Distinct operation across a Where call.
1 parent f139ddf commit e446cc9

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

src/MongoDB.Driver/Linq/Processors/Pipeline/MethodCallBinders/DistinctBinder.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ public Expression Bind(PipelineExpression pipeline, PipelineBindingContext bindi
4545
keySelector = fieldAsDocumentExpression.Expression;
4646
}
4747
}
48+
else if (pipeline.Projector is FieldExpression)
49+
{
50+
keySelector = pipeline.Projector;
51+
}
4852
else
4953
{
5054
var currentProjector = (ISerializationExpression)pipeline.Projector;

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

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public async Task CountAsync_with_no_matches()
179179
}
180180

181181
[SkippableFact]
182-
public void Distinct_followed_by_where()
182+
public void Distinct_document_followed_by_where()
183183
{
184184
RequireServer.Where(minimumVersion: "2.6.0");
185185
var query = CreateQuery()
@@ -192,6 +192,68 @@ public void Distinct_followed_by_where()
192192
"{ $match: { '_id.A': 'Awesome' } }");
193193
}
194194

195+
[SkippableFact]
196+
public void Distinct_document_preceded_by_select_where()
197+
{
198+
RequireServer.Where(minimumVersion: "2.6.0");
199+
var query = CreateQuery()
200+
.Select(x => new { x.A, x.B })
201+
.Where(x => x.A == "Awesome")
202+
.Distinct();
203+
204+
Assert(query,
205+
1,
206+
"{ $project: { 'A': '$A', 'B': '$B', '_id': 0 } }",
207+
"{ $match: { 'A': 'Awesome' } }",
208+
"{ $group: { '_id': '$$ROOT' } }");
209+
}
210+
211+
[SkippableFact]
212+
public void Distinct_document_preceded_by_where_select()
213+
{
214+
RequireServer.Where(minimumVersion: "2.6.0");
215+
var query = CreateQuery()
216+
.Where(x => x.A == "Awesome")
217+
.Select(x => new { x.A, x.B })
218+
.Distinct();
219+
220+
Assert(query,
221+
1,
222+
"{ $match: { 'A': 'Awesome' } }",
223+
"{ $group: { '_id': { 'A': '$A', 'B': '$B' } } }");
224+
}
225+
226+
[SkippableFact]
227+
public void Distinct_field_preceded_by_where_select()
228+
{
229+
RequireServer.Where(minimumVersion: "2.6.0");
230+
var query = CreateQuery()
231+
.Where(x => x.A == "Awesome")
232+
.Select(x => x.A)
233+
.Distinct();
234+
235+
Assert(query,
236+
1,
237+
"{ $match: { 'A': 'Awesome' } }",
238+
"{ $group: { '_id': '$A' } }");
239+
}
240+
241+
[SkippableFact]
242+
public void Distinct_field_preceded_by_select_where()
243+
{
244+
RequireServer.Where(minimumVersion: "2.6.0");
245+
var query = CreateQuery()
246+
.Select(x => x.A)
247+
.Where(x => x == "Awesome")
248+
.Distinct();
249+
250+
Assert(query,
251+
1,
252+
"{ $project: { 'A': '$A', '_id': 0 } }",
253+
"{ $match: { 'A': 'Awesome' } }",
254+
"{ $group: { '_id': '$A' } }");
255+
}
256+
195257
[Fact]
196258
public void First()
197259
{

0 commit comments

Comments
 (0)