Skip to content

Commit 699018e

Browse files
committed
CSHARP-1433: added support for includeArrayIndex in the $unwind stage.
1 parent 68b9c3e commit 699018e

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

src/MongoDB.Driver.Tests/IAggregateFluentExtensionsTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,32 @@ public void Unwind_with_options_with_preserveNullAndEmptyArrays_set()
231231
AssertLast(subject, expectedUnwind);
232232
}
233233

234+
[Test]
235+
public void Unwind_with_options_with_includeArrayIndex_set()
236+
{
237+
var subject = CreateSubject()
238+
.Unwind("Age", new AggregateUnwindOptions<BsonDocument> { IncludeArrayIndex = "AgeIndex" });
239+
240+
var expectedUnwind = BsonDocument.Parse("{$unwind: { path: '$Age', includeArrayIndex: 'AgeIndex' } }");
241+
242+
AssertLast(subject, expectedUnwind);
243+
}
244+
245+
[Test]
246+
public void Unwind_with_options_with_includeArrayIndex_set_and_preserveNullAndEmptyArrays_set()
247+
{
248+
var subject = CreateSubject()
249+
.Unwind("Age", new AggregateUnwindOptions<BsonDocument>
250+
{
251+
IncludeArrayIndex = "AgeIndex",
252+
PreserveNullAndEmptyArrays = true
253+
});
254+
255+
var expectedUnwind = BsonDocument.Parse("{$unwind: { path: '$Age', preserveNullAndEmptyArrays: true, includeArrayIndex: 'AgeIndex' } }");
256+
257+
AssertLast(subject, expectedUnwind);
258+
}
259+
234260
private void AssertLast<TDocument>(IAggregateFluent<TDocument> fluent, BsonDocument expectedLast)
235261
{
236262
var pipeline = new PipelineStagePipelineDefinition<Person, TDocument>(fluent.Stages);

src/MongoDB.Driver/AggregateFluent.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,20 +204,29 @@ public override IAggregateFluent<TNewResult> Unwind<TNewResult>(FieldDefinition<
204204
operatorName,
205205
(s, sr) =>
206206
{
207+
var newResultSerializer = options.ResultSerializer ?? (s as IBsonSerializer<TNewResult>) ?? sr.GetSerializer<TNewResult>();
208+
207209
var fieldName = "$" + field.Render(s, sr).FieldName;
210+
string includeArrayIndexFieldName = null;
211+
if (options.IncludeArrayIndex != null)
212+
{
213+
includeArrayIndexFieldName = options.IncludeArrayIndex.Render(newResultSerializer, sr).FieldName;
214+
}
215+
208216
BsonValue value = fieldName;
209-
if (options.PreserveNullAndEmptyArrays.HasValue)
217+
if (options.PreserveNullAndEmptyArrays.HasValue || includeArrayIndexFieldName != null)
210218
{
211219
value = new BsonDocument
212220
{
213221
{ "path", fieldName },
214-
{ "preserveNullAndEmptyArrays", options.PreserveNullAndEmptyArrays, options.PreserveNullAndEmptyArrays.HasValue }
222+
{ "preserveNullAndEmptyArrays", options.PreserveNullAndEmptyArrays, options.PreserveNullAndEmptyArrays.HasValue },
223+
{ "includeArrayIndex", includeArrayIndexFieldName, includeArrayIndexFieldName != null }
215224
};
216225
}
217226
return new RenderedPipelineStageDefinition<TNewResult>(
218227
operatorName,
219228
new BsonDocument(operatorName, value),
220-
options.ResultSerializer ?? (s as IBsonSerializer<TNewResult>) ?? sr.GetSerializer<TNewResult>());
229+
newResultSerializer);
221230
});
222231

223232
return AppendStage<TNewResult>(stage);

src/MongoDB.Driver/AggregateUnwindOptions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,19 @@ namespace MongoDB.Driver
2222
/// </summary>
2323
public class AggregateUnwindOptions<TResult>
2424
{
25+
private FieldDefinition<TResult> _includeArrayIndex;
2526
private bool? _preserveNullAndEmptyArrays;
2627
private IBsonSerializer<TResult> _resultSerializer;
2728

29+
/// <summary>
30+
/// Gets or sets the field with which to include the array index.
31+
/// </summary>
32+
public FieldDefinition<TResult> IncludeArrayIndex
33+
{
34+
get { return _includeArrayIndex; }
35+
set { _includeArrayIndex = value; }
36+
}
37+
2838
/// <summary>
2939
/// Gets or sets whether to preserve null and empty arrays.
3040
/// </summary>

0 commit comments

Comments
 (0)