Skip to content

Commit 76d04c3

Browse files
DATAMONGO-1391 - Polishing.
Removed white spaces, updated Javadoc and return early when using non 3.2 $unwind options. Original Pull Request: #355
1 parent 460ab05 commit 76d04c3

File tree

4 files changed

+71
-33
lines changed

4 files changed

+71
-33
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Aggregation.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* @author Oliver Gierke
4343
* @author Mark Paluch
4444
* @author Alessio Fachechi
45+
* @author Christoph Strobl
4546
* @since 1.3
4647
*/
4748
public class Aggregation {
@@ -207,36 +208,37 @@ public static UnwindOperation unwind(String field) {
207208
* @param field must not be {@literal null} or empty.
208209
* @param preserveNullAndEmptyArrays {@literal true} to output the document if path is {@literal null}, missing or
209210
* array is empty.
210-
* @return
211+
* @return new {@link UnwindOperation}
211212
* @since 1.10
212213
*/
213214
public static UnwindOperation unwind(String field, boolean preserveNullAndEmptyArrays) {
214215
return new UnwindOperation(field(field), preserveNullAndEmptyArrays);
215216
}
216217

217218
/**
218-
* Factory method to create a new {@link UnwindOperation} for the field with the given name and to include the index
219-
* field as {@code arrayIndex}. Note that extended unwind is supported in MongoDB version 3.2+.
219+
* Factory method to create a new {@link UnwindOperation} for the field with the given name including the name of a
220+
* new field to hold the array index of the element as {@code arrayIndex}. Note that extended unwind is supported in
221+
* MongoDB version 3.2+.
220222
*
221223
* @param field must not be {@literal null} or empty.
222224
* @param arrayIndex must not be {@literal null} or empty.
223-
* @return
225+
* @return new {@link UnwindOperation}
224226
* @since 1.10
225227
*/
226228
public static UnwindOperation unwind(String field, String arrayIndex) {
227229
return new UnwindOperation(field(field), field(arrayIndex), false);
228230
}
229231

230232
/**
231-
* Factory method to create a new {@link UnwindOperation} for the field with the given name, to include the index
232-
* field as {@code arrayIndex} and {@code preserveNullAndEmptyArrays}. Note that extended unwind is supported in
233-
* MongoDB version 3.2+.
233+
* Factory method to create a new {@link UnwindOperation} for the field with the given nameincluding the name of a new
234+
* field to hold the array index of the element as {@code arrayIndex} using {@code preserveNullAndEmptyArrays}. Note
235+
* that extended unwind is supported in MongoDB version 3.2+.
234236
*
235237
* @param field must not be {@literal null} or empty.
236238
* @param arrayIndex must not be {@literal null} or empty.
237239
* @param preserveNullAndEmptyArrays {@literal true} to output the document if path is {@literal null}, missing or
238240
* array is empty.
239-
* @return
241+
* @return new {@link UnwindOperation}
240242
* @since 1.10
241243
*/
242244
public static UnwindOperation unwind(String field, String arrayIndex, boolean preserveNullAndEmptyArrays) {

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/UnwindOperation.java

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* @author Thomas Darimont
3030
* @author Oliver Gierke
3131
* @author Mark Paluch
32+
* @author Christoph Strobl
3233
* @since 1.3
3334
*/
3435
public class UnwindOperation
@@ -40,7 +41,7 @@ public class UnwindOperation
4041

4142
/**
4243
* Creates a new {@link UnwindOperation} for the given {@link Field}.
43-
*
44+
*
4445
* @param field must not be {@literal null}.
4546
*/
4647
public UnwindOperation(Field field) {
@@ -49,7 +50,7 @@ public UnwindOperation(Field field) {
4950

5051
/**
5152
* Creates a new {@link UnwindOperation} using Mongo 3.2 syntax.
52-
*
53+
*
5354
* @param field must not be {@literal null}.
5455
* @param preserveNullAndEmptyArrays {@literal true} to output the document if path is {@literal null}, missing or
5556
* array is empty.
@@ -65,7 +66,7 @@ public UnwindOperation(Field field, boolean preserveNullAndEmptyArrays) {
6566

6667
/**
6768
* Creates a new {@link UnwindOperation} using Mongo 3.2 syntax.
68-
*
69+
*
6970
* @param field must not be {@literal null}.
7071
* @param arrayIndex optional field name to expose the field array index, must not be {@literal null}.
7172
* @param preserveNullAndEmptyArrays {@literal true} to output the document if path is {@literal null}, missing or
@@ -89,26 +90,26 @@ public UnwindOperation(Field field, Field arrayIndex, boolean preserveNullAndEmp
8990
@Override
9091
public Document toDocument(AggregationOperationContext context) {
9192

92-
String unwindField = context.getReference(field).toString();
93-
Object unwindArg;
94-
95-
if (preserveNullAndEmptyArrays || arrayIndex != null) {
96-
97-
Document builder = new Document().append("path", unwindField);
98-
builder = builder.append("preserveNullAndEmptyArrays", preserveNullAndEmptyArrays);
93+
String path = context.getReference(field).toString();
9994

100-
if (arrayIndex != null) {
101-
builder = builder.append("includeArrayIndex", arrayIndex.getName());
102-
}
95+
if (!preserveNullAndEmptyArrays && arrayIndex == null) {
96+
return new Document("$unwind", path);
97+
}
10398

104-
unwindArg = builder;
105-
} else {
106-
unwindArg = unwindField;
99+
Document unwindArgs = new Document();
100+
unwindArgs.put("path", path);
101+
if (arrayIndex != null) {
102+
unwindArgs.put("includeArrayIndex", arrayIndex.getName());
107103
}
104+
unwindArgs.put("preserveNullAndEmptyArrays", preserveNullAndEmptyArrays);
108105

109-
return new Document("$unwind", unwindArg);
106+
return new Document("$unwind", unwindArgs);
110107
}
111108

109+
/*
110+
* (non-Javadoc)
111+
* @see org.springframework.data.mongodb.core.aggregation.FieldsExposingAggregationOperation#getFields()
112+
*/
112113
@Override
113114
public ExposedFields getFields() {
114115
return arrayIndex != null ? ExposedFields.from(arrayIndex) : ExposedFields.from();
@@ -118,11 +119,16 @@ public ExposedFields getFields() {
118119
* Get a builder that allows creation of {@link LookupOperation}.
119120
*
120121
* @return
122+
* @since 1.10
121123
*/
122124
public static PathBuilder newUnwind() {
123125
return UnwindOperationBuilder.newBuilder();
124126
}
125127

128+
/**
129+
* @author Mark Paluch
130+
* @since 1.10
131+
*/
126132
public static interface PathBuilder {
127133

128134
/**
@@ -132,19 +138,23 @@ public static interface PathBuilder {
132138
IndexBuilder path(String path);
133139
}
134140

141+
/**
142+
* @author Mark Paluch
143+
* @since 1.10
144+
*/
135145
public static interface IndexBuilder {
136146

137147
/**
138148
* Exposes the array index as {@code field}.
139-
*
149+
*
140150
* @param field field name to expose the field array index, must not be {@literal null} or empty.
141151
* @return
142152
*/
143153
EmptyArraysBuilder arrayIndex(String field);
144154

145155
/**
146156
* Do not expose the array index.
147-
*
157+
*
148158
* @return
149159
*/
150160
EmptyArraysBuilder noArrayIndex();
@@ -154,14 +164,14 @@ public static interface EmptyArraysBuilder {
154164

155165
/**
156166
* Output documents if the array is null or empty.
157-
*
167+
*
158168
* @return
159169
*/
160170
UnwindOperation preserveNullAndEmptyArrays();
161171

162172
/**
163173
* Do not output documents if the array is null or empty.
164-
*
174+
*
165175
* @return
166176
*/
167177
UnwindOperation skipNullAndEmptyArrays();
@@ -189,6 +199,10 @@ public static PathBuilder newBuilder() {
189199
return new UnwindOperationBuilder();
190200
}
191201

202+
/*
203+
* (non-Javadoc)
204+
* @see org.springframework.data.mongodb.core.aggregation.UnwindOperation.EmptyArraysBuilder#preserveNullAndEmptyArrays()
205+
*/
192206
@Override
193207
public UnwindOperation preserveNullAndEmptyArrays() {
194208

@@ -199,6 +213,10 @@ public UnwindOperation preserveNullAndEmptyArrays() {
199213
return new UnwindOperation(field, true);
200214
}
201215

216+
/*
217+
* (non-Javadoc)
218+
* @see org.springframework.data.mongodb.core.aggregation.UnwindOperation.EmptyArraysBuilder#skipNullAndEmptyArrays()
219+
*/
202220
@Override
203221
public UnwindOperation skipNullAndEmptyArrays() {
204222

@@ -209,26 +227,39 @@ public UnwindOperation skipNullAndEmptyArrays() {
209227
return new UnwindOperation(field, false);
210228
}
211229

230+
/*
231+
* (non-Javadoc)
232+
* @see org.springframework.data.mongodb.core.aggregation.UnwindOperation.IndexBuilder#arrayIndex(java.lang.String)
233+
*/
212234
@Override
213235
public EmptyArraysBuilder arrayIndex(String field) {
236+
214237
Assert.hasText(field, "'ArrayIndex' must not be null or empty!");
215238
arrayIndex = Fields.field(field);
216239
return this;
217240
}
218241

242+
/*
243+
* (non-Javadoc)
244+
* @see org.springframework.data.mongodb.core.aggregation.UnwindOperation.IndexBuilder#noArrayIndex()
245+
*/
219246
@Override
220247
public EmptyArraysBuilder noArrayIndex() {
248+
221249
arrayIndex = null;
222250
return this;
223251
}
224252

253+
/*
254+
* (non-Javadoc)
255+
* @see org.springframework.data.mongodb.core.aggregation.UnwindOperation.PathBuilder#path(java.lang.String)
256+
*/
225257
@Override
226258
public UnwindOperationBuilder path(String path) {
259+
227260
Assert.hasText(path, "'Path' must not be null or empty!");
228261
field = Fields.field(path);
229262
return this;
230263
}
231-
232264
}
233-
234265
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ public void shouldAggregateEmptyCollection() {
244244
@Test
245245
public void shouldUnwindWithIndex() {
246246

247+
assumeTrue(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_TWO));
248+
247249
MongoCollection<Document> coll = mongoTemplate.getCollection(INPUT_COLLECTION);
248250

249251
coll.insertOne(createDocument("Doc1", "spring", "mongodb", "nosql"));
@@ -273,6 +275,8 @@ public void shouldUnwindWithIndex() {
273275
@Test
274276
public void shouldUnwindPreserveEmpty() {
275277

278+
assumeTrue(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_TWO));
279+
276280
MongoCollection<Document> coll = mongoTemplate.getCollection(INPUT_COLLECTION);
277281

278282
coll.insertOne(createDocument("Doc1", "spring", "mongodb", "nosql"));

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/UnwindOperationUnitTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525

2626
/**
2727
* Unit tests for {@link UnwindOperation}.
28-
*
28+
*
2929
* @author Mark Paluch
30+
* @author Christoph Strobl
3031
*/
3132
public class UnwindOperationUnitTests {
3233

@@ -123,7 +124,7 @@ public void lookupBuilderBuildsCorrectClauseForMongo32() {
123124
assertThat(unwindClause,
124125
isBsonObject().containing("path", "$foo").//
125126
containing("preserveNullAndEmptyArrays", true).//
126-
notContaining("myindex"));
127+
containing("includeArrayIndex", "myindex"));
127128
}
128129

129130
private Document extractDbObjectFromUnwindOperation(UnwindOperation unwindOperation) {

0 commit comments

Comments
 (0)