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