DATAMONGO-1391 - Polishing.
Removed white spaces, updated Javadoc and return early when using non 3.2 $unwind options. Original Pull Request: #355
This commit is contained in:
@@ -44,6 +44,7 @@ import com.mongodb.DBObject;
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
* @author Alessio Fachechi
|
||||
* @author Christoph Strobl
|
||||
* @since 1.3
|
||||
*/
|
||||
public class Aggregation {
|
||||
@@ -209,7 +210,7 @@ public class Aggregation {
|
||||
* @param field must not be {@literal null} or empty.
|
||||
* @param preserveNullAndEmptyArrays {@literal true} to output the document if path is {@literal null}, missing or
|
||||
* array is empty.
|
||||
* @return
|
||||
* @return new {@link UnwindOperation}
|
||||
* @since 1.10
|
||||
*/
|
||||
public static UnwindOperation unwind(String field, boolean preserveNullAndEmptyArrays) {
|
||||
@@ -217,12 +218,13 @@ public class Aggregation {
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method to create a new {@link UnwindOperation} for the field with the given name and to include the index
|
||||
* field as {@code arrayIndex}. Note that extended unwind is supported in MongoDB version 3.2+.
|
||||
* Factory method to create a new {@link UnwindOperation} for the field with the given name including the name of a
|
||||
* new field to hold the array index of the element as {@code arrayIndex}. Note that extended unwind is supported in
|
||||
* MongoDB version 3.2+.
|
||||
*
|
||||
* @param field must not be {@literal null} or empty.
|
||||
* @param arrayIndex must not be {@literal null} or empty.
|
||||
* @return
|
||||
* @return new {@link UnwindOperation}
|
||||
* @since 1.10
|
||||
*/
|
||||
public static UnwindOperation unwind(String field, String arrayIndex) {
|
||||
@@ -230,15 +232,15 @@ public class Aggregation {
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method to create a new {@link UnwindOperation} for the field with the given name, to include the index
|
||||
* field as {@code arrayIndex} and {@code preserveNullAndEmptyArrays}. Note that extended unwind is supported in
|
||||
* MongoDB version 3.2+.
|
||||
* Factory method to create a new {@link UnwindOperation} for the field with the given nameincluding the name of a new
|
||||
* field to hold the array index of the element as {@code arrayIndex} using {@code preserveNullAndEmptyArrays}. Note
|
||||
* that extended unwind is supported in MongoDB version 3.2+.
|
||||
*
|
||||
* @param field must not be {@literal null} or empty.
|
||||
* @param arrayIndex must not be {@literal null} or empty.
|
||||
* @param preserveNullAndEmptyArrays {@literal true} to output the document if path is {@literal null}, missing or
|
||||
* array is empty.
|
||||
* @return
|
||||
* @return new {@link UnwindOperation}
|
||||
* @since 1.10
|
||||
*/
|
||||
public static UnwindOperation unwind(String field, String arrayIndex, boolean preserveNullAndEmptyArrays) {
|
||||
|
||||
@@ -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;
|
||||
* @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
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
||||
/**
|
||||
* 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
|
||||
@Override
|
||||
public DBObject toDBObject(AggregationOperationContext context) {
|
||||
|
||||
String unwindField = context.getReference(field).toString();
|
||||
Object unwindArg;
|
||||
String path = context.getReference(field).toString();
|
||||
|
||||
if (preserveNullAndEmptyArrays || arrayIndex != null) {
|
||||
|
||||
BasicDBObjectBuilder builder = BasicDBObjectBuilder.start().add("path", unwindField);
|
||||
builder.add("preserveNullAndEmptyArrays", preserveNullAndEmptyArrays);
|
||||
|
||||
if (arrayIndex != null) {
|
||||
builder.add("includeArrayIndex", arrayIndex.getName());
|
||||
}
|
||||
|
||||
unwindArg = builder.get();
|
||||
} else {
|
||||
unwindArg = unwindField;
|
||||
if (!preserveNullAndEmptyArrays && arrayIndex == null) {
|
||||
return new BasicDBObject("$unwind", path);
|
||||
}
|
||||
|
||||
return new BasicDBObject("$unwind", unwindArg);
|
||||
DBObject unwindArgs = new BasicDBObject();
|
||||
unwindArgs.put("path", path);
|
||||
if (arrayIndex != null) {
|
||||
unwindArgs.put("includeArrayIndex", arrayIndex.getName());
|
||||
}
|
||||
unwindArgs.put("preserveNullAndEmptyArrays", preserveNullAndEmptyArrays);
|
||||
|
||||
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
|
||||
* 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
|
||||
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
|
||||
|
||||
/**
|
||||
* Do not expose the array index.
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
EmptyArraysBuilder noArrayIndex();
|
||||
@@ -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
|
||||
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
|
||||
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
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -247,6 +247,8 @@ public class AggregationTests {
|
||||
@Test
|
||||
public void shouldUnwindWithIndex() {
|
||||
|
||||
assumeTrue(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_TWO));
|
||||
|
||||
DBCollection coll = mongoTemplate.getCollection(INPUT_COLLECTION);
|
||||
|
||||
coll.insert(createDocument("Doc1", "spring", "mongodb", "nosql"));
|
||||
@@ -276,6 +278,8 @@ public class AggregationTests {
|
||||
@Test
|
||||
public void shouldUnwindPreserveEmpty() {
|
||||
|
||||
assumeTrue(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_TWO));
|
||||
|
||||
DBCollection coll = mongoTemplate.getCollection(INPUT_COLLECTION);
|
||||
|
||||
coll.insert(createDocument("Doc1", "spring", "mongodb", "nosql"));
|
||||
|
||||
@@ -26,8 +26,9 @@ import com.mongodb.DBObject;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link UnwindOperation}.
|
||||
*
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
public class UnwindOperationUnitTests {
|
||||
|
||||
@@ -124,7 +125,7 @@ public class UnwindOperationUnitTests {
|
||||
assertThat(unwindClause,
|
||||
isBsonObject().containing("path", "$foo").//
|
||||
containing("preserveNullAndEmptyArrays", true).//
|
||||
notContaining("myindex"));
|
||||
containing("includeArrayIndex", "myindex"));
|
||||
}
|
||||
|
||||
private DBObject extractDbObjectFromUnwindOperation(UnwindOperation unwindOperation) {
|
||||
|
||||
Reference in New Issue
Block a user