29
29
* @author Thomas Darimont
30
30
* @author Oliver Gierke
31
31
* @author Mark Paluch
32
+ * @author Christoph Strobl
32
33
* @since 1.3
33
34
*/
34
35
public class UnwindOperation
@@ -40,7 +41,7 @@ public class UnwindOperation
40
41
41
42
/**
42
43
* Creates a new {@link UnwindOperation} for the given {@link Field}.
43
- *
44
+ *
44
45
* @param field must not be {@literal null}.
45
46
*/
46
47
public UnwindOperation (Field field ) {
@@ -49,7 +50,7 @@ public UnwindOperation(Field field) {
49
50
50
51
/**
51
52
* Creates a new {@link UnwindOperation} using Mongo 3.2 syntax.
52
- *
53
+ *
53
54
* @param field must not be {@literal null}.
54
55
* @param preserveNullAndEmptyArrays {@literal true} to output the document if path is {@literal null}, missing or
55
56
* array is empty.
@@ -65,7 +66,7 @@ public UnwindOperation(Field field, boolean preserveNullAndEmptyArrays) {
65
66
66
67
/**
67
68
* Creates a new {@link UnwindOperation} using Mongo 3.2 syntax.
68
- *
69
+ *
69
70
* @param field must not be {@literal null}.
70
71
* @param arrayIndex optional field name to expose the field array index, must not be {@literal null}.
71
72
* @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
89
90
@ Override
90
91
public Document toDocument (AggregationOperationContext context ) {
91
92
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 ();
99
94
100
- if (arrayIndex ! = null ) {
101
- builder = builder . append ( "includeArrayIndex " , arrayIndex . getName () );
102
- }
95
+ if (! preserveNullAndEmptyArrays && arrayIndex = = null ) {
96
+ return new Document ( "$unwind " , path );
97
+ }
103
98
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 ());
107
103
}
104
+ unwindArgs .put ("preserveNullAndEmptyArrays" , preserveNullAndEmptyArrays );
108
105
109
- return new Document ("$unwind" , unwindArg );
106
+ return new Document ("$unwind" , unwindArgs );
110
107
}
111
108
109
+ /*
110
+ * (non-Javadoc)
111
+ * @see org.springframework.data.mongodb.core.aggregation.FieldsExposingAggregationOperation#getFields()
112
+ */
112
113
@ Override
113
114
public ExposedFields getFields () {
114
115
return arrayIndex != null ? ExposedFields .from (arrayIndex ) : ExposedFields .from ();
@@ -118,11 +119,16 @@ public ExposedFields getFields() {
118
119
* Get a builder that allows creation of {@link LookupOperation}.
119
120
*
120
121
* @return
122
+ * @since 1.10
121
123
*/
122
124
public static PathBuilder newUnwind () {
123
125
return UnwindOperationBuilder .newBuilder ();
124
126
}
125
127
128
+ /**
129
+ * @author Mark Paluch
130
+ * @since 1.10
131
+ */
126
132
public static interface PathBuilder {
127
133
128
134
/**
@@ -132,19 +138,23 @@ public static interface PathBuilder {
132
138
IndexBuilder path (String path );
133
139
}
134
140
141
+ /**
142
+ * @author Mark Paluch
143
+ * @since 1.10
144
+ */
135
145
public static interface IndexBuilder {
136
146
137
147
/**
138
148
* Exposes the array index as {@code field}.
139
- *
149
+ *
140
150
* @param field field name to expose the field array index, must not be {@literal null} or empty.
141
151
* @return
142
152
*/
143
153
EmptyArraysBuilder arrayIndex (String field );
144
154
145
155
/**
146
156
* Do not expose the array index.
147
- *
157
+ *
148
158
* @return
149
159
*/
150
160
EmptyArraysBuilder noArrayIndex ();
@@ -154,14 +164,14 @@ public static interface EmptyArraysBuilder {
154
164
155
165
/**
156
166
* Output documents if the array is null or empty.
157
- *
167
+ *
158
168
* @return
159
169
*/
160
170
UnwindOperation preserveNullAndEmptyArrays ();
161
171
162
172
/**
163
173
* Do not output documents if the array is null or empty.
164
- *
174
+ *
165
175
* @return
166
176
*/
167
177
UnwindOperation skipNullAndEmptyArrays ();
@@ -189,6 +199,10 @@ public static PathBuilder newBuilder() {
189
199
return new UnwindOperationBuilder ();
190
200
}
191
201
202
+ /*
203
+ * (non-Javadoc)
204
+ * @see org.springframework.data.mongodb.core.aggregation.UnwindOperation.EmptyArraysBuilder#preserveNullAndEmptyArrays()
205
+ */
192
206
@ Override
193
207
public UnwindOperation preserveNullAndEmptyArrays () {
194
208
@@ -199,6 +213,10 @@ public UnwindOperation preserveNullAndEmptyArrays() {
199
213
return new UnwindOperation (field , true );
200
214
}
201
215
216
+ /*
217
+ * (non-Javadoc)
218
+ * @see org.springframework.data.mongodb.core.aggregation.UnwindOperation.EmptyArraysBuilder#skipNullAndEmptyArrays()
219
+ */
202
220
@ Override
203
221
public UnwindOperation skipNullAndEmptyArrays () {
204
222
@@ -209,26 +227,39 @@ public UnwindOperation skipNullAndEmptyArrays() {
209
227
return new UnwindOperation (field , false );
210
228
}
211
229
230
+ /*
231
+ * (non-Javadoc)
232
+ * @see org.springframework.data.mongodb.core.aggregation.UnwindOperation.IndexBuilder#arrayIndex(java.lang.String)
233
+ */
212
234
@ Override
213
235
public EmptyArraysBuilder arrayIndex (String field ) {
236
+
214
237
Assert .hasText (field , "'ArrayIndex' must not be null or empty!" );
215
238
arrayIndex = Fields .field (field );
216
239
return this ;
217
240
}
218
241
242
+ /*
243
+ * (non-Javadoc)
244
+ * @see org.springframework.data.mongodb.core.aggregation.UnwindOperation.IndexBuilder#noArrayIndex()
245
+ */
219
246
@ Override
220
247
public EmptyArraysBuilder noArrayIndex () {
248
+
221
249
arrayIndex = null ;
222
250
return this ;
223
251
}
224
252
253
+ /*
254
+ * (non-Javadoc)
255
+ * @see org.springframework.data.mongodb.core.aggregation.UnwindOperation.PathBuilder#path(java.lang.String)
256
+ */
225
257
@ Override
226
258
public UnwindOperationBuilder path (String path ) {
259
+
227
260
Assert .hasText (path , "'Path' must not be null or empty!" );
228
261
field = Fields .field (path );
229
262
return this ;
230
263
}
231
-
232
264
}
233
-
234
265
}
0 commit comments