|
|
|
|
@ -19,7 +19,6 @@ import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedFi
@@ -19,7 +19,6 @@ import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedFi
|
|
|
|
|
import org.springframework.util.Assert; |
|
|
|
|
|
|
|
|
|
import com.mongodb.BasicDBObject; |
|
|
|
|
import com.mongodb.BasicDBObjectBuilder; |
|
|
|
|
import com.mongodb.DBObject; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -32,6 +31,7 @@ import com.mongodb.DBObject;
@@ -32,6 +31,7 @@ import com.mongodb.DBObject;
|
|
|
|
|
* @author Thomas Darimont |
|
|
|
|
* @author Oliver Gierke |
|
|
|
|
* @author Mark Paluch |
|
|
|
|
* @author Christoph Strobl |
|
|
|
|
* @since 1.3 |
|
|
|
|
*/ |
|
|
|
|
public class UnwindOperation |
|
|
|
|
@ -43,7 +43,7 @@ public class UnwindOperation
@@ -43,7 +43,7 @@ public class UnwindOperation
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Creates a new {@link UnwindOperation} for the given {@link Field}. |
|
|
|
|
* |
|
|
|
|
* |
|
|
|
|
* @param field must not be {@literal null}. |
|
|
|
|
*/ |
|
|
|
|
public UnwindOperation(Field field) { |
|
|
|
|
@ -52,7 +52,7 @@ public class UnwindOperation
@@ -52,7 +52,7 @@ public class UnwindOperation
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Creates a new {@link UnwindOperation} using Mongo 3.2 syntax. |
|
|
|
|
* |
|
|
|
|
* |
|
|
|
|
* @param field must not be {@literal null}. |
|
|
|
|
* @param preserveNullAndEmptyArrays {@literal true} to output the document if path is {@literal null}, missing or |
|
|
|
|
* array is empty. |
|
|
|
|
@ -68,7 +68,7 @@ public class UnwindOperation
@@ -68,7 +68,7 @@ public class UnwindOperation
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Creates a new {@link UnwindOperation} using Mongo 3.2 syntax. |
|
|
|
|
* |
|
|
|
|
* |
|
|
|
|
* @param field must not be {@literal null}. |
|
|
|
|
* @param arrayIndex optional field name to expose the field array index, must not be {@literal null}. |
|
|
|
|
* @param preserveNullAndEmptyArrays {@literal true} to output the document if path is {@literal null}, missing or |
|
|
|
|
@ -92,26 +92,26 @@ public class UnwindOperation
@@ -92,26 +92,26 @@ public class UnwindOperation
|
|
|
|
|
@Override |
|
|
|
|
public DBObject toDBObject(AggregationOperationContext context) { |
|
|
|
|
|
|
|
|
|
String unwindField = context.getReference(field).toString(); |
|
|
|
|
Object unwindArg; |
|
|
|
|
|
|
|
|
|
if (preserveNullAndEmptyArrays || arrayIndex != null) { |
|
|
|
|
|
|
|
|
|
BasicDBObjectBuilder builder = BasicDBObjectBuilder.start().add("path", unwindField); |
|
|
|
|
builder.add("preserveNullAndEmptyArrays", preserveNullAndEmptyArrays); |
|
|
|
|
String path = context.getReference(field).toString(); |
|
|
|
|
|
|
|
|
|
if (arrayIndex != null) { |
|
|
|
|
builder.add("includeArrayIndex", arrayIndex.getName()); |
|
|
|
|
} |
|
|
|
|
if (!preserveNullAndEmptyArrays && arrayIndex == null) { |
|
|
|
|
return new BasicDBObject("$unwind", path); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
unwindArg = builder.get(); |
|
|
|
|
} else { |
|
|
|
|
unwindArg = unwindField; |
|
|
|
|
DBObject unwindArgs = new BasicDBObject(); |
|
|
|
|
unwindArgs.put("path", path); |
|
|
|
|
if (arrayIndex != null) { |
|
|
|
|
unwindArgs.put("includeArrayIndex", arrayIndex.getName()); |
|
|
|
|
} |
|
|
|
|
unwindArgs.put("preserveNullAndEmptyArrays", preserveNullAndEmptyArrays); |
|
|
|
|
|
|
|
|
|
return new BasicDBObject("$unwind", unwindArg); |
|
|
|
|
return new BasicDBObject("$unwind", unwindArgs); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
* (non-Javadoc) |
|
|
|
|
* @see org.springframework.data.mongodb.core.aggregation.FieldsExposingAggregationOperation#getFields() |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public ExposedFields getFields() { |
|
|
|
|
return arrayIndex != null ? ExposedFields.from(arrayIndex) : ExposedFields.from(); |
|
|
|
|
@ -121,11 +121,16 @@ public class UnwindOperation
@@ -121,11 +121,16 @@ public class UnwindOperation
|
|
|
|
|
* Get a builder that allows creation of {@link LookupOperation}. |
|
|
|
|
* |
|
|
|
|
* @return |
|
|
|
|
* @since 1.10 |
|
|
|
|
*/ |
|
|
|
|
public static PathBuilder newUnwind() { |
|
|
|
|
return UnwindOperationBuilder.newBuilder(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @author Mark Paluch |
|
|
|
|
* @since 1.10 |
|
|
|
|
*/ |
|
|
|
|
public static interface PathBuilder { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
@ -135,11 +140,15 @@ public class UnwindOperation
@@ -135,11 +140,15 @@ public class UnwindOperation
|
|
|
|
|
IndexBuilder path(String path); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @author Mark Paluch |
|
|
|
|
* @since 1.10 |
|
|
|
|
*/ |
|
|
|
|
public static interface IndexBuilder { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Exposes the array index as {@code field}. |
|
|
|
|
* |
|
|
|
|
* |
|
|
|
|
* @param field field name to expose the field array index, must not be {@literal null} or empty. |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
@ -147,7 +156,7 @@ public class UnwindOperation
@@ -147,7 +156,7 @@ public class UnwindOperation
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Do not expose the array index. |
|
|
|
|
* |
|
|
|
|
* |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
EmptyArraysBuilder noArrayIndex(); |
|
|
|
|
@ -157,14 +166,14 @@ public class UnwindOperation
@@ -157,14 +166,14 @@ public class UnwindOperation
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Output documents if the array is null or empty. |
|
|
|
|
* |
|
|
|
|
* |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
UnwindOperation preserveNullAndEmptyArrays(); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Do not output documents if the array is null or empty. |
|
|
|
|
* |
|
|
|
|
* |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
UnwindOperation skipNullAndEmptyArrays(); |
|
|
|
|
@ -192,6 +201,10 @@ public class UnwindOperation
@@ -192,6 +201,10 @@ public class UnwindOperation
|
|
|
|
|
return new UnwindOperationBuilder(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
* (non-Javadoc) |
|
|
|
|
* @see org.springframework.data.mongodb.core.aggregation.UnwindOperation.EmptyArraysBuilder#preserveNullAndEmptyArrays() |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public UnwindOperation preserveNullAndEmptyArrays() { |
|
|
|
|
|
|
|
|
|
@ -202,6 +215,10 @@ public class UnwindOperation
@@ -202,6 +215,10 @@ public class UnwindOperation
|
|
|
|
|
return new UnwindOperation(field, true); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
* (non-Javadoc) |
|
|
|
|
* @see org.springframework.data.mongodb.core.aggregation.UnwindOperation.EmptyArraysBuilder#skipNullAndEmptyArrays() |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public UnwindOperation skipNullAndEmptyArrays() { |
|
|
|
|
|
|
|
|
|
@ -212,26 +229,39 @@ public class UnwindOperation
@@ -212,26 +229,39 @@ public class UnwindOperation
|
|
|
|
|
return new UnwindOperation(field, false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
* (non-Javadoc) |
|
|
|
|
* @see org.springframework.data.mongodb.core.aggregation.UnwindOperation.IndexBuilder#arrayIndex(java.lang.String) |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public EmptyArraysBuilder arrayIndex(String field) { |
|
|
|
|
|
|
|
|
|
Assert.hasText(field, "'ArrayIndex' must not be null or empty!"); |
|
|
|
|
arrayIndex = Fields.field(field); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
* (non-Javadoc) |
|
|
|
|
* @see org.springframework.data.mongodb.core.aggregation.UnwindOperation.IndexBuilder#noArrayIndex() |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public EmptyArraysBuilder noArrayIndex() { |
|
|
|
|
|
|
|
|
|
arrayIndex = null; |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
* (non-Javadoc) |
|
|
|
|
* @see org.springframework.data.mongodb.core.aggregation.UnwindOperation.PathBuilder#path(java.lang.String) |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public UnwindOperationBuilder path(String path) { |
|
|
|
|
|
|
|
|
|
Assert.hasText(path, "'Path' must not be null or empty!"); |
|
|
|
|
field = Fields.field(path); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|