|
1 | 1 | /*
|
2 |
| - * Copyright 2013-2015 the original author or authors. |
| 2 | + * Copyright 2013-2016 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
28 | 28 | * @see http://docs.mongodb.org/manual/reference/aggregation/unwind/#pipe._S_unwind
|
29 | 29 | * @author Thomas Darimont
|
30 | 30 | * @author Oliver Gierke
|
| 31 | + * @author Mark Paluch |
31 | 32 | * @since 1.3
|
32 | 33 | */
|
33 |
| -public class UnwindOperation implements AggregationOperation { |
| 34 | +public class UnwindOperation |
| 35 | + implements AggregationOperation, FieldsExposingAggregationOperation.InheritsFieldsAggregationOperation { |
34 | 36 |
|
35 | 37 | private final ExposedField field;
|
| 38 | + private final ExposedField arrayIndex; |
| 39 | + private final boolean preserveNullAndEmptyArrays; |
36 | 40 |
|
37 | 41 | /**
|
38 | 42 | * Creates a new {@link UnwindOperation} for the given {@link Field}.
|
39 | 43 | *
|
40 | 44 | * @param field must not be {@literal null}.
|
41 | 45 | */
|
42 | 46 | public UnwindOperation(Field field) {
|
| 47 | + this(new ExposedField(field, true), false); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Creates a new {@link UnwindOperation} using Mongo 3.2 syntax. |
| 52 | + * |
| 53 | + * @param field must not be {@literal null}. |
| 54 | + * @param preserveNullAndEmptyArrays {@literal true} to output the document if path is {@literal null}, missing or |
| 55 | + * array is empty. |
| 56 | + * @since 1.10 |
| 57 | + */ |
| 58 | + public UnwindOperation(Field field, boolean preserveNullAndEmptyArrays) { |
| 59 | + Assert.notNull(field, "Field must not be null!"); |
| 60 | + |
| 61 | + this.field = new ExposedField(field, true); |
| 62 | + this.arrayIndex = null; |
| 63 | + this.preserveNullAndEmptyArrays = preserveNullAndEmptyArrays; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Creates a new {@link UnwindOperation} using Mongo 3.2 syntax. |
| 68 | + * |
| 69 | + * @param field must not be {@literal null}. |
| 70 | + * @param arrayIndex optional field name to expose the field array index, must not be {@literal null}. |
| 71 | + * @param preserveNullAndEmptyArrays {@literal true} to output the document if path is {@literal null}, missing or |
| 72 | + * array is empty. |
| 73 | + * @since 1.10 |
| 74 | + */ |
| 75 | + public UnwindOperation(Field field, Field arrayIndex, boolean preserveNullAndEmptyArrays) { |
| 76 | + |
| 77 | + Assert.notNull(field, "Field must not be null!"); |
| 78 | + Assert.notNull(arrayIndex, "ArrayIndex must not be null!"); |
43 | 79 |
|
44 |
| - Assert.notNull(field); |
45 | 80 | this.field = new ExposedField(field, true);
|
| 81 | + this.arrayIndex = new ExposedField(arrayIndex, true); |
| 82 | + this.preserveNullAndEmptyArrays = preserveNullAndEmptyArrays; |
46 | 83 | }
|
47 | 84 |
|
48 |
| - /* |
| 85 | + /* |
49 | 86 | * (non-Javadoc)
|
50 | 87 | * @see org.springframework.data.mongodb.core.aggregation.AggregationOperation#toDocument(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext)
|
51 | 88 | */
|
52 | 89 | @Override
|
53 | 90 | public Document toDocument(AggregationOperationContext context) {
|
54 |
| - return new Document("$unwind", context.getReference(field).toString()); |
| 91 | + |
| 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); |
| 99 | + |
| 100 | + if (arrayIndex != null) { |
| 101 | + builder = builder.append("includeArrayIndex", arrayIndex.getName()); |
| 102 | + } |
| 103 | + |
| 104 | + unwindArg = builder; |
| 105 | + } else { |
| 106 | + unwindArg = unwindField; |
| 107 | + } |
| 108 | + |
| 109 | + return new Document("$unwind", unwindArg); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public ExposedFields getFields() { |
| 114 | + return arrayIndex != null ? ExposedFields.from(arrayIndex) : ExposedFields.from(); |
55 | 115 | }
|
| 116 | + |
| 117 | + /** |
| 118 | + * Get a builder that allows creation of {@link LookupOperation}. |
| 119 | + * |
| 120 | + * @return |
| 121 | + */ |
| 122 | + public static PathBuilder newUnwind() { |
| 123 | + return UnwindOperationBuilder.newBuilder(); |
| 124 | + } |
| 125 | + |
| 126 | + public static interface PathBuilder { |
| 127 | + |
| 128 | + /** |
| 129 | + * @param path the path to unwind, must not be {@literal null} or empty. |
| 130 | + * @return |
| 131 | + */ |
| 132 | + IndexBuilder path(String path); |
| 133 | + } |
| 134 | + |
| 135 | + public static interface IndexBuilder { |
| 136 | + |
| 137 | + /** |
| 138 | + * Exposes the array index as {@code field}. |
| 139 | + * |
| 140 | + * @param field field name to expose the field array index, must not be {@literal null} or empty. |
| 141 | + * @return |
| 142 | + */ |
| 143 | + EmptyArraysBuilder arrayIndex(String field); |
| 144 | + |
| 145 | + /** |
| 146 | + * Do not expose the array index. |
| 147 | + * |
| 148 | + * @return |
| 149 | + */ |
| 150 | + EmptyArraysBuilder noArrayIndex(); |
| 151 | + } |
| 152 | + |
| 153 | + public static interface EmptyArraysBuilder { |
| 154 | + |
| 155 | + /** |
| 156 | + * Output documents if the array is null or empty. |
| 157 | + * |
| 158 | + * @return |
| 159 | + */ |
| 160 | + UnwindOperation preserveNullAndEmptyArrays(); |
| 161 | + |
| 162 | + /** |
| 163 | + * Do not output documents if the array is null or empty. |
| 164 | + * |
| 165 | + * @return |
| 166 | + */ |
| 167 | + UnwindOperation skipNullAndEmptyArrays(); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Builder for fluent {@link UnwindOperation} creation. |
| 172 | + * |
| 173 | + * @author Mark Paluch |
| 174 | + * @since 1.10 |
| 175 | + */ |
| 176 | + public static final class UnwindOperationBuilder implements PathBuilder, IndexBuilder, EmptyArraysBuilder { |
| 177 | + |
| 178 | + private Field field; |
| 179 | + private Field arrayIndex; |
| 180 | + |
| 181 | + private UnwindOperationBuilder() {} |
| 182 | + |
| 183 | + /** |
| 184 | + * Creates new builder for {@link UnwindOperation}. |
| 185 | + * |
| 186 | + * @return never {@literal null}. |
| 187 | + */ |
| 188 | + public static PathBuilder newBuilder() { |
| 189 | + return new UnwindOperationBuilder(); |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + public UnwindOperation preserveNullAndEmptyArrays() { |
| 194 | + |
| 195 | + if (arrayIndex != null) { |
| 196 | + return new UnwindOperation(field, arrayIndex, true); |
| 197 | + } |
| 198 | + |
| 199 | + return new UnwindOperation(field, true); |
| 200 | + } |
| 201 | + |
| 202 | + @Override |
| 203 | + public UnwindOperation skipNullAndEmptyArrays() { |
| 204 | + |
| 205 | + if (arrayIndex != null) { |
| 206 | + return new UnwindOperation(field, arrayIndex, false); |
| 207 | + } |
| 208 | + |
| 209 | + return new UnwindOperation(field, false); |
| 210 | + } |
| 211 | + |
| 212 | + @Override |
| 213 | + public EmptyArraysBuilder arrayIndex(String field) { |
| 214 | + Assert.hasText(field, "'ArrayIndex' must not be null or empty!"); |
| 215 | + arrayIndex = Fields.field(field); |
| 216 | + return this; |
| 217 | + } |
| 218 | + |
| 219 | + @Override |
| 220 | + public EmptyArraysBuilder noArrayIndex() { |
| 221 | + arrayIndex = null; |
| 222 | + return this; |
| 223 | + } |
| 224 | + |
| 225 | + @Override |
| 226 | + public UnwindOperationBuilder path(String path) { |
| 227 | + Assert.hasText(path, "'Path' must not be null or empty!"); |
| 228 | + field = Fields.field(path); |
| 229 | + return this; |
| 230 | + } |
| 231 | + |
| 232 | + } |
| 233 | + |
56 | 234 | }
|
0 commit comments