Skip to content

Commit 5485f2f

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 f8681fe commit 5485f2f

File tree

4 files changed

+71
-34
lines changed

4 files changed

+71
-34
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
@@ -44,6 +44,7 @@
4444
* @author Oliver Gierke
4545
* @author Mark Paluch
4646
* @author Alessio Fachechi
47+
* @author Christoph Strobl
4748
* @since 1.3
4849
*/
4950
public class Aggregation {
@@ -209,36 +210,37 @@ public static UnwindOperation unwind(String field) {
209210
* @param field must not be {@literal null} or empty.
210211
* @param preserveNullAndEmptyArrays {@literal true} to output the document if path is {@literal null}, missing or
211212
* array is empty.
212-
* @return
213+
* @return new {@link UnwindOperation}
213214
* @since 1.10
214215
*/
215216
public static UnwindOperation unwind(String field, boolean preserveNullAndEmptyArrays) {
216217
return new UnwindOperation(field(field), preserveNullAndEmptyArrays);
217218
}
218219

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

232234
/**
233-
* Factory method to create a new {@link UnwindOperation} for the field with the given name, to include the index
234-
* field as {@code arrayIndex} and {@code preserveNullAndEmptyArrays}. Note that extended unwind is supported in
235-
* MongoDB version 3.2+.
235+
* Factory method to create a new {@link UnwindOperation} for the field with the given nameincluding the name of a new
236+
* field to hold the array index of the element as {@code arrayIndex} using {@code preserveNullAndEmptyArrays}. Note
237+
* that extended unwind is supported in MongoDB version 3.2+.
236238
*
237239
* @param field must not be {@literal null} or empty.
238240
* @param arrayIndex must not be {@literal null} or empty.
239241
* @param preserveNullAndEmptyArrays {@literal true} to output the document if path is {@literal null}, missing or
240242
* array is empty.
241-
* @return
243+
* @return new {@link UnwindOperation}
242244
* @since 1.10
243245
*/
244246
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 & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.springframework.util.Assert;
2020

2121
import com.mongodb.BasicDBObject;
22-
import com.mongodb.BasicDBObjectBuilder;
2322
import com.mongodb.DBObject;
2423

2524
/**
@@ -32,6 +31,7 @@
3231
* @author Thomas Darimont
3332
* @author Oliver Gierke
3433
* @author Mark Paluch
34+
* @author Christoph Strobl
3535
* @since 1.3
3636
*/
3737
public class UnwindOperation
@@ -43,7 +43,7 @@ public class UnwindOperation
4343

4444
/**
4545
* Creates a new {@link UnwindOperation} for the given {@link Field}.
46-
*
46+
*
4747
* @param field must not be {@literal null}.
4848
*/
4949
public UnwindOperation(Field field) {
@@ -52,7 +52,7 @@ public UnwindOperation(Field field) {
5252

5353
/**
5454
* Creates a new {@link UnwindOperation} using Mongo 3.2 syntax.
55-
*
55+
*
5656
* @param field must not be {@literal null}.
5757
* @param preserveNullAndEmptyArrays {@literal true} to output the document if path is {@literal null}, missing or
5858
* array is empty.
@@ -68,7 +68,7 @@ public UnwindOperation(Field field, boolean preserveNullAndEmptyArrays) {
6868

6969
/**
7070
* Creates a new {@link UnwindOperation} using Mongo 3.2 syntax.
71-
*
71+
*
7272
* @param field must not be {@literal null}.
7373
* @param arrayIndex optional field name to expose the field array index, must not be {@literal null}.
7474
* @param preserveNullAndEmptyArrays {@literal true} to output the document if path is {@literal null}, missing or
@@ -92,26 +92,26 @@ public UnwindOperation(Field field, Field arrayIndex, boolean preserveNullAndEmp
9292
@Override
9393
public DBObject toDBObject(AggregationOperationContext context) {
9494

95-
String unwindField = context.getReference(field).toString();
96-
Object unwindArg;
97-
98-
if (preserveNullAndEmptyArrays || arrayIndex != null) {
99-
100-
BasicDBObjectBuilder builder = BasicDBObjectBuilder.start().add("path", unwindField);
101-
builder.add("preserveNullAndEmptyArrays", preserveNullAndEmptyArrays);
95+
String path = context.getReference(field).toString();
10296

103-
if (arrayIndex != null) {
104-
builder.add("includeArrayIndex", arrayIndex.getName());
105-
}
97+
if (!preserveNullAndEmptyArrays && arrayIndex == null) {
98+
return new BasicDBObject("$unwind", path);
99+
}
106100

107-
unwindArg = builder.get();
108-
} else {
109-
unwindArg = unwindField;
101+
DBObject unwindArgs = new BasicDBObject();
102+
unwindArgs.put("path", path);
103+
if (arrayIndex != null) {
104+
unwindArgs.put("includeArrayIndex", arrayIndex.getName());
110105
}
106+
unwindArgs.put("preserveNullAndEmptyArrays", preserveNullAndEmptyArrays);
111107

112-
return new BasicDBObject("$unwind", unwindArg);
108+
return new BasicDBObject("$unwind", unwindArgs);
113109
}
114110

111+
/*
112+
* (non-Javadoc)
113+
* @see org.springframework.data.mongodb.core.aggregation.FieldsExposingAggregationOperation#getFields()
114+
*/
115115
@Override
116116
public ExposedFields getFields() {
117117
return arrayIndex != null ? ExposedFields.from(arrayIndex) : ExposedFields.from();
@@ -121,11 +121,16 @@ public ExposedFields getFields() {
121121
* Get a builder that allows creation of {@link LookupOperation}.
122122
*
123123
* @return
124+
* @since 1.10
124125
*/
125126
public static PathBuilder newUnwind() {
126127
return UnwindOperationBuilder.newBuilder();
127128
}
128129

130+
/**
131+
* @author Mark Paluch
132+
* @since 1.10
133+
*/
129134
public static interface PathBuilder {
130135

131136
/**
@@ -135,19 +140,23 @@ public static interface PathBuilder {
135140
IndexBuilder path(String path);
136141
}
137142

143+
/**
144+
* @author Mark Paluch
145+
* @since 1.10
146+
*/
138147
public static interface IndexBuilder {
139148

140149
/**
141150
* Exposes the array index as {@code field}.
142-
*
151+
*
143152
* @param field field name to expose the field array index, must not be {@literal null} or empty.
144153
* @return
145154
*/
146155
EmptyArraysBuilder arrayIndex(String field);
147156

148157
/**
149158
* Do not expose the array index.
150-
*
159+
*
151160
* @return
152161
*/
153162
EmptyArraysBuilder noArrayIndex();
@@ -157,14 +166,14 @@ public static interface EmptyArraysBuilder {
157166

158167
/**
159168
* Output documents if the array is null or empty.
160-
*
169+
*
161170
* @return
162171
*/
163172
UnwindOperation preserveNullAndEmptyArrays();
164173

165174
/**
166175
* Do not output documents if the array is null or empty.
167-
*
176+
*
168177
* @return
169178
*/
170179
UnwindOperation skipNullAndEmptyArrays();
@@ -192,6 +201,10 @@ public static PathBuilder newBuilder() {
192201
return new UnwindOperationBuilder();
193202
}
194203

204+
/*
205+
* (non-Javadoc)
206+
* @see org.springframework.data.mongodb.core.aggregation.UnwindOperation.EmptyArraysBuilder#preserveNullAndEmptyArrays()
207+
*/
195208
@Override
196209
public UnwindOperation preserveNullAndEmptyArrays() {
197210

@@ -202,6 +215,10 @@ public UnwindOperation preserveNullAndEmptyArrays() {
202215
return new UnwindOperation(field, true);
203216
}
204217

218+
/*
219+
* (non-Javadoc)
220+
* @see org.springframework.data.mongodb.core.aggregation.UnwindOperation.EmptyArraysBuilder#skipNullAndEmptyArrays()
221+
*/
205222
@Override
206223
public UnwindOperation skipNullAndEmptyArrays() {
207224

@@ -212,26 +229,39 @@ public UnwindOperation skipNullAndEmptyArrays() {
212229
return new UnwindOperation(field, false);
213230
}
214231

232+
/*
233+
* (non-Javadoc)
234+
* @see org.springframework.data.mongodb.core.aggregation.UnwindOperation.IndexBuilder#arrayIndex(java.lang.String)
235+
*/
215236
@Override
216237
public EmptyArraysBuilder arrayIndex(String field) {
238+
217239
Assert.hasText(field, "'ArrayIndex' must not be null or empty!");
218240
arrayIndex = Fields.field(field);
219241
return this;
220242
}
221243

244+
/*
245+
* (non-Javadoc)
246+
* @see org.springframework.data.mongodb.core.aggregation.UnwindOperation.IndexBuilder#noArrayIndex()
247+
*/
222248
@Override
223249
public EmptyArraysBuilder noArrayIndex() {
250+
224251
arrayIndex = null;
225252
return this;
226253
}
227254

255+
/*
256+
* (non-Javadoc)
257+
* @see org.springframework.data.mongodb.core.aggregation.UnwindOperation.PathBuilder#path(java.lang.String)
258+
*/
228259
@Override
229260
public UnwindOperationBuilder path(String path) {
261+
230262
Assert.hasText(path, "'Path' must not be null or empty!");
231263
field = Fields.field(path);
232264
return this;
233265
}
234-
235266
}
236-
237267
}

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
@@ -247,6 +247,8 @@ public void shouldAggregateEmptyCollection() {
247247
@Test
248248
public void shouldUnwindWithIndex() {
249249

250+
assumeTrue(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_TWO));
251+
250252
DBCollection coll = mongoTemplate.getCollection(INPUT_COLLECTION);
251253

252254
coll.insert(createDocument("Doc1", "spring", "mongodb", "nosql"));
@@ -276,6 +278,8 @@ public void shouldUnwindWithIndex() {
276278
@Test
277279
public void shouldUnwindPreserveEmpty() {
278280

281+
assumeTrue(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_TWO));
282+
279283
DBCollection coll = mongoTemplate.getCollection(INPUT_COLLECTION);
280284

281285
coll.insert(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
@@ -26,8 +26,9 @@
2626

2727
/**
2828
* Unit tests for {@link UnwindOperation}.
29-
*
29+
*
3030
* @author Mark Paluch
31+
* @author Christoph Strobl
3132
*/
3233
public class UnwindOperationUnitTests {
3334

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

130131
private DBObject extractDbObjectFromUnwindOperation(UnwindOperation unwindOperation) {

0 commit comments

Comments
 (0)