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