Browse Source

DATAMONGO-1893 - Polishing.

Inherit fields from previous operation if at least one field is excluded. Extend FieldsExposingAggregationOperation to conditionally inherit fields.

Backport to Java 6 code.

Original pull request: #538.
pull/553/head
Mark Paluch 8 years ago
parent
commit
e8d3c9e932
  1. 4
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AggregationOperationRenderer.java
  2. 10
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/BucketOperationSupport.java
  3. 10
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/CountOperation.java
  4. 10
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/FacetOperation.java
  5. 13
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/FieldsExposingAggregationOperation.java
  6. 10
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GraphLookupOperation.java
  7. 60
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GroupOperation.java
  8. 10
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/LookupOperation.java
  9. 157
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java
  10. 10
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ReplaceRootOperation.java
  11. 10
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/UnwindOperation.java
  12. 48
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperationUnitTests.java
  13. 29
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/TypeBasedAggregationOperationContextUnitTests.java

4
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/AggregationOperationRenderer.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2016 the original author or authors. * Copyright 2016-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -60,7 +60,7 @@ class AggregationOperationRenderer {
FieldsExposingAggregationOperation exposedFieldsOperation = (FieldsExposingAggregationOperation) operation; FieldsExposingAggregationOperation exposedFieldsOperation = (FieldsExposingAggregationOperation) operation;
ExposedFields fields = exposedFieldsOperation.getFields(); ExposedFields fields = exposedFieldsOperation.getFields();
if (operation instanceof InheritsFieldsAggregationOperation) { if (operation instanceof InheritsFieldsAggregationOperation || exposedFieldsOperation.inheritsFields()) {
contextToUse = new InheritingExposedFieldsAggregationOperationContext(fields, contextToUse); contextToUse = new InheritingExposedFieldsAggregationOperationContext(fields, contextToUse);
} else { } else {
contextToUse = fields.exposesNoFields() ? DEFAULT_CONTEXT contextToUse = fields.exposesNoFields() ? DEFAULT_CONTEXT

10
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/BucketOperationSupport.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2016 the original author or authors. * Copyright 2016-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -169,6 +169,14 @@ public abstract class BucketOperationSupport<T extends BucketOperationSupport<T,
return outputs.asExposedFields(); return outputs.asExposedFields();
} }
/* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.FieldsExposingAggregationOperation#inheritsFields()
*/
@Override
public boolean inheritsFields() {
return false;
}
/** /**
* Implementation hook to create a new bucket operation. * Implementation hook to create a new bucket operation.
* *

10
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/CountOperation.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2016 the original author or authors. * Copyright 2016-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -62,6 +62,14 @@ public class CountOperation implements FieldsExposingAggregationOperation {
return ExposedFields.from(new ExposedField(fieldName, true)); return ExposedFields.from(new ExposedField(fieldName, true));
} }
/* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.FieldsExposingAggregationOperation#inheritsFields()
*/
@Override
public boolean inheritsFields() {
return false;
}
/** /**
* Builder for {@link CountOperation}. * Builder for {@link CountOperation}.
* *

10
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/FacetOperation.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2016-2017 the original author or authors. * Copyright 2016-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -96,6 +96,14 @@ public class FacetOperation implements FieldsExposingAggregationOperation {
return facets.asExposedFields(); return facets.asExposedFields();
} }
/* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.FieldsExposingAggregationOperation#inheritsFields()
*/
@Override
public boolean inheritsFields() {
return false;
}
/** /**
* Builder for {@link FacetOperation} by adding existing and the new pipeline of {@link AggregationOperation} to the * Builder for {@link FacetOperation} by adding existing and the new pipeline of {@link AggregationOperation} to the
* new {@link FacetOperation}. * new {@link FacetOperation}.

13
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/FieldsExposingAggregationOperation.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2013-2016 the original author or authors. * Copyright 2013-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -34,10 +34,13 @@ public interface FieldsExposingAggregationOperation extends AggregationOperation
ExposedFields getFields(); ExposedFields getFields();
/** /**
* Marker interface for {@link AggregationOperation} that inherits fields from previous operations. * @return {@literal true} to conditionally inherit fields from previous operations.
* @since 1.10.12
*/ */
static interface InheritsFieldsAggregationOperation extends FieldsExposingAggregationOperation { boolean inheritsFields();
}
/**
* Marker interface for {@link AggregationOperation} that inherits fields from previous operations.
*/
static interface InheritsFieldsAggregationOperation extends FieldsExposingAggregationOperation {}
} }

10
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GraphLookupOperation.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2016-2017 the original author or authors. * Copyright 2016-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -132,6 +132,14 @@ public class GraphLookupOperation implements InheritsFieldsAggregationOperation
return ExposedFields.from(new ExposedField(as, true)); return ExposedFields.from(new ExposedField(as, true));
} }
/* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.FieldsExposingAggregationOperation#inheritsFields()
*/
@Override
public boolean inheritsFields() {
return true;
}
/** /**
* @author Mark Paluch * @author Mark Paluch
*/ */

60
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GroupOperation.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2013-2017 the original author or authors. * Copyright 2013-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,7 +32,7 @@ import com.mongodb.DBObject;
* <p> * <p>
* We recommend to use the static factory method {@link Aggregation#group(Fields)} instead of creating instances of this * We recommend to use the static factory method {@link Aggregation#group(Fields)} instead of creating instances of this
* class directly. * class directly.
* *
* @author Sebastian Herold * @author Sebastian Herold
* @author Thomas Darimont * @author Thomas Darimont
* @author Oliver Gierke * @author Oliver Gierke
@ -53,7 +53,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
/** /**
* Creates a new {@link GroupOperation} including the given {@link Fields}. * Creates a new {@link GroupOperation} including the given {@link Fields}.
* *
* @param fields must not be {@literal null}. * @param fields must not be {@literal null}.
*/ */
public GroupOperation(Fields fields) { public GroupOperation(Fields fields) {
@ -64,7 +64,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
/** /**
* Creates a new {@link GroupOperation} from the given {@link GroupOperation}. * Creates a new {@link GroupOperation} from the given {@link GroupOperation}.
* *
* @param groupOperation must not be {@literal null}. * @param groupOperation must not be {@literal null}.
*/ */
protected GroupOperation(GroupOperation groupOperation) { protected GroupOperation(GroupOperation groupOperation) {
@ -73,7 +73,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
/** /**
* Creates a new {@link GroupOperation} from the given {@link GroupOperation} and the given {@link Operation}s. * Creates a new {@link GroupOperation} from the given {@link GroupOperation} and the given {@link Operation}s.
* *
* @param groupOperation * @param groupOperation
* @param nextOperations * @param nextOperations
*/ */
@ -90,7 +90,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
/** /**
* Creates a new {@link GroupOperation} from the current one adding the given {@link Operation}. * Creates a new {@link GroupOperation} from the current one adding the given {@link Operation}.
* *
* @param operation must not be {@literal null}. * @param operation must not be {@literal null}.
* @return * @return
*/ */
@ -100,7 +100,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
/** /**
* Builder for {@link GroupOperation}s on a field. * Builder for {@link GroupOperation}s on a field.
* *
* @author Thomas Darimont * @author Thomas Darimont
*/ */
public static final class GroupOperationBuilder { public static final class GroupOperationBuilder {
@ -110,7 +110,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
/** /**
* Creates a new {@link GroupOperationBuilder} from the given {@link GroupOperation} and {@link Operation}. * Creates a new {@link GroupOperationBuilder} from the given {@link GroupOperation} and {@link Operation}.
* *
* @param groupOperation * @param groupOperation
* @param operation * @param operation
*/ */
@ -125,7 +125,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
/** /**
* Allows to specify an alias for the new-operation operation. * Allows to specify an alias for the new-operation operation.
* *
* @param alias * @param alias
* @return * @return
*/ */
@ -139,7 +139,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
* <p> * <p>
* Count expressions are emulated via {@code $sum: 1}. * Count expressions are emulated via {@code $sum: 1}.
* <p> * <p>
* *
* @return * @return
*/ */
public GroupOperationBuilder count() { public GroupOperationBuilder count() {
@ -148,7 +148,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
/** /**
* Generates an {@link GroupOperationBuilder} for a {@code $sum}-expression for the given field-reference. * Generates an {@link GroupOperationBuilder} for a {@code $sum}-expression for the given field-reference.
* *
* @param reference * @param reference
* @return * @return
*/ */
@ -177,7 +177,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
/** /**
* Generates an {@link GroupOperationBuilder} for an {@code $add_to_set}-expression for the given field-reference. * Generates an {@link GroupOperationBuilder} for an {@code $add_to_set}-expression for the given field-reference.
* *
* @param reference * @param reference
* @return * @return
*/ */
@ -187,7 +187,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
/** /**
* Generates an {@link GroupOperationBuilder} for an {@code $add_to_set}-expression for the given value. * Generates an {@link GroupOperationBuilder} for an {@code $add_to_set}-expression for the given value.
* *
* @param value * @param value
* @return * @return
*/ */
@ -201,7 +201,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
/** /**
* Generates an {@link GroupOperationBuilder} for an {@code $last}-expression for the given field-reference. * Generates an {@link GroupOperationBuilder} for an {@code $last}-expression for the given field-reference.
* *
* @param reference * @param reference
* @return * @return
*/ */
@ -212,7 +212,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
/** /**
* Generates an {@link GroupOperationBuilder} for an {@code $last}-expression for the given * Generates an {@link GroupOperationBuilder} for an {@code $last}-expression for the given
* {@link AggregationExpression}. * {@link AggregationExpression}.
* *
* @param expr * @param expr
* @return * @return
*/ */
@ -222,7 +222,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
/** /**
* Generates an {@link GroupOperationBuilder} for a {@code $first}-expression for the given field-reference. * Generates an {@link GroupOperationBuilder} for a {@code $first}-expression for the given field-reference.
* *
* @param reference * @param reference
* @return * @return
*/ */
@ -233,7 +233,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
/** /**
* Generates an {@link GroupOperationBuilder} for a {@code $first}-expression for the given * Generates an {@link GroupOperationBuilder} for a {@code $first}-expression for the given
* {@link AggregationExpression}. * {@link AggregationExpression}.
* *
* @param expr * @param expr
* @return * @return
*/ */
@ -243,7 +243,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
/** /**
* Generates an {@link GroupOperationBuilder} for an {@code $avg}-expression for the given field-reference. * Generates an {@link GroupOperationBuilder} for an {@code $avg}-expression for the given field-reference.
* *
* @param reference * @param reference
* @return * @return
*/ */
@ -254,7 +254,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
/** /**
* Generates an {@link GroupOperationBuilder} for an {@code $avg}-expression for the given * Generates an {@link GroupOperationBuilder} for an {@code $avg}-expression for the given
* {@link AggregationExpression}. * {@link AggregationExpression}.
* *
* @param expr * @param expr
* @return * @return
*/ */
@ -264,7 +264,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
/** /**
* Generates an {@link GroupOperationBuilder} for an {@code $push}-expression for the given field-reference. * Generates an {@link GroupOperationBuilder} for an {@code $push}-expression for the given field-reference.
* *
* @param reference * @param reference
* @return * @return
*/ */
@ -274,7 +274,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
/** /**
* Generates an {@link GroupOperationBuilder} for an {@code $push}-expression for the given value. * Generates an {@link GroupOperationBuilder} for an {@code $push}-expression for the given value.
* *
* @param value * @param value
* @return * @return
*/ */
@ -288,7 +288,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
/** /**
* Generates an {@link GroupOperationBuilder} for an {@code $min}-expression that for the given field-reference. * Generates an {@link GroupOperationBuilder} for an {@code $min}-expression that for the given field-reference.
* *
* @param reference * @param reference
* @return * @return
*/ */
@ -299,7 +299,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
/** /**
* Generates an {@link GroupOperationBuilder} for an {@code $min}-expression that for the given * Generates an {@link GroupOperationBuilder} for an {@code $min}-expression that for the given
* {@link AggregationExpression}. * {@link AggregationExpression}.
* *
* @param expr * @param expr
* @return * @return
*/ */
@ -309,7 +309,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
/** /**
* Generates an {@link GroupOperationBuilder} for an {@code $max}-expression that for the given field-reference. * Generates an {@link GroupOperationBuilder} for an {@code $max}-expression that for the given field-reference.
* *
* @param reference * @param reference
* @return * @return
*/ */
@ -320,7 +320,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
/** /**
* Generates an {@link GroupOperationBuilder} for an {@code $max}-expression that for the given * Generates an {@link GroupOperationBuilder} for an {@code $max}-expression that for the given
* {@link AggregationExpression}. * {@link AggregationExpression}.
* *
* @param expr * @param expr
* @return * @return
*/ */
@ -379,7 +379,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
return new GroupOperationBuilder(this, new Operation(keyword, null, reference, value)); return new GroupOperationBuilder(this, new Operation(keyword, null, reference, value));
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.AggregationOperationContext#getFields() * @see org.springframework.data.mongodb.core.aggregation.AggregationOperationContext#getFields()
*/ */
@ -395,6 +395,14 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
return fields; return fields;
} }
/* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.FieldsExposingAggregationOperation#inheritsFields()
*/
@Override
public boolean inheritsFields() {
return false;
}
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.AggregationOperation#toDBObject(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext) * @see org.springframework.data.mongodb.core.aggregation.AggregationOperation#toDBObject(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext)

10
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/LookupOperation.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2016-2017 the original author or authors. * Copyright 2016-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -73,6 +73,14 @@ public class LookupOperation implements FieldsExposingAggregationOperation, Inhe
return ExposedFields.from(as); return ExposedFields.from(as);
} }
/* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.FieldsExposingAggregationOperation#inheritsFields()
*/
@Override
public boolean inheritsFields() {
return true;
}
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.AggregationOperation#toDBObject(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext) * @see org.springframework.data.mongodb.core.aggregation.AggregationOperation#toDBObject(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext)

157
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2013-2017 the original author or authors. * Copyright 2013-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -40,20 +40,19 @@ import com.mongodb.DBObject;
* <p> * <p>
* We recommend to use the static factory method {@link Aggregation#project(Fields)} instead of creating instances of * We recommend to use the static factory method {@link Aggregation#project(Fields)} instead of creating instances of
* this class directly. * this class directly.
* *
* @author Tobias Trelle * @author Tobias Trelle
* @author Thomas Darimont * @author Thomas Darimont
* @author Oliver Gierke * @author Oliver Gierke
* @author Christoph Strobl * @author Christoph Strobl
* @author Mark Paluch * @author Mark Paluch
* @since 1.3 * @since 1.3
* @see <a href="https://docs.mongodb.com/manual/reference/operator/aggregation/project/">MongoDB Aggregation Framework: $project</a> * @see <a href="https://docs.mongodb.com/manual/reference/operator/aggregation/project/">MongoDB Aggregation Framework:
* $project</a>
*/ */
public class ProjectionOperation implements FieldsExposingAggregationOperation { public class ProjectionOperation implements FieldsExposingAggregationOperation {
private static final List<Projection> NONE = Collections.emptyList(); private static final List<Projection> NONE = Collections.emptyList();
private static final String EXCLUSION_ERROR = "Exclusion of field %s not allowed. Projections by the mongodb "
+ "aggregation framework only support the exclusion of the %s field!";
private final List<Projection> projections; private final List<Projection> projections;
@ -66,7 +65,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Creates a new {@link ProjectionOperation} including the given {@link Fields}. * Creates a new {@link ProjectionOperation} including the given {@link Fields}.
* *
* @param fields must not be {@literal null}. * @param fields must not be {@literal null}.
*/ */
public ProjectionOperation(Fields fields) { public ProjectionOperation(Fields fields) {
@ -76,7 +75,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Copy constructor to allow building up {@link ProjectionOperation} instances from already existing * Copy constructor to allow building up {@link ProjectionOperation} instances from already existing
* {@link Projection}s. * {@link Projection}s.
* *
* @param current must not be {@literal null}. * @param current must not be {@literal null}.
* @param projections must not be {@literal null}. * @param projections must not be {@literal null}.
*/ */
@ -92,7 +91,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Creates a new {@link ProjectionOperation} with the current {@link Projection}s and the given one. * Creates a new {@link ProjectionOperation} with the current {@link Projection}s and the given one.
* *
* @param projection must not be {@literal null}. * @param projection must not be {@literal null}.
* @return * @return
*/ */
@ -103,7 +102,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Creates a new {@link ProjectionOperation} with the current {@link Projection}s replacing the last current one with * Creates a new {@link ProjectionOperation} with the current {@link Projection}s replacing the last current one with
* the given one. * the given one.
* *
* @param projection must not be {@literal null}. * @param projection must not be {@literal null}.
* @return * @return
*/ */
@ -116,7 +115,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Creates a new {@link ProjectionOperationBuilder} to define a projection for the field with the given name. * Creates a new {@link ProjectionOperationBuilder} to define a projection for the field with the given name.
* *
* @param name must not be {@literal null} or empty. * @param name must not be {@literal null} or empty.
* @return * @return
*/ */
@ -134,7 +133,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Excludes the given fields from the projection. * Excludes the given fields from the projection.
* *
* @param fieldNames must not be {@literal null}. * @param fieldNames must not be {@literal null}.
* @return * @return
*/ */
@ -146,7 +145,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Includes the given fields into the projection. * Includes the given fields into the projection.
* *
* @param fieldNames must not be {@literal null}. * @param fieldNames must not be {@literal null}.
* @return * @return
*/ */
@ -158,7 +157,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Includes the given fields into the projection. * Includes the given fields into the projection.
* *
* @param fields must not be {@literal null}. * @param fields must not be {@literal null}.
* @return * @return
*/ */
@ -183,6 +182,25 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
return fields; return fields;
} }
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.FieldsExposingAggregationOperation#inheritsFields()
*/
@Override
public boolean inheritsFields() {
for (Projection projection : projections) {
if (projection instanceof FieldProjection) {
if (((FieldProjection) projection).isExcluded()) {
return true;
}
}
}
return false;
}
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.AggregationOperation#toDBObject(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext) * @see org.springframework.data.mongodb.core.aggregation.AggregationOperation#toDBObject(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext)
@ -201,7 +219,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Base class for {@link ProjectionOperationBuilder}s. * Base class for {@link ProjectionOperationBuilder}s.
* *
* @author Thomas Darimont * @author Thomas Darimont
*/ */
private static abstract class AbstractProjectionOperationBuilder implements AggregationOperation { private static abstract class AbstractProjectionOperationBuilder implements AggregationOperation {
@ -211,7 +229,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Creates a new {@link AbstractProjectionOperationBuilder} fot the given value and {@link ProjectionOperation}. * Creates a new {@link AbstractProjectionOperationBuilder} fot the given value and {@link ProjectionOperation}.
* *
* @param value must not be {@literal null}. * @param value must not be {@literal null}.
* @param operation must not be {@literal null}. * @param operation must not be {@literal null}.
*/ */
@ -224,7 +242,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
this.operation = operation; this.operation = operation;
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.AggregationOperation#toDBObject(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext) * @see org.springframework.data.mongodb.core.aggregation.AggregationOperation#toDBObject(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext)
*/ */
@ -235,7 +253,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Returns the finally to be applied {@link ProjectionOperation} with the given alias. * Returns the finally to be applied {@link ProjectionOperation} with the given alias.
* *
* @param alias will never be {@literal null} or empty. * @param alias will never be {@literal null} or empty.
* @return * @return
*/ */
@ -262,7 +280,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* An {@link ProjectionOperationBuilder} that is used for SpEL expression based projections. * An {@link ProjectionOperationBuilder} that is used for SpEL expression based projections.
* *
* @author Thomas Darimont * @author Thomas Darimont
*/ */
public static class ExpressionProjectionOperationBuilder extends ProjectionOperationBuilder { public static class ExpressionProjectionOperationBuilder extends ProjectionOperationBuilder {
@ -273,7 +291,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Creates a new {@link ExpressionProjectionOperationBuilder} for the given value, {@link ProjectionOperation} and * Creates a new {@link ExpressionProjectionOperationBuilder} for the given value, {@link ProjectionOperation} and
* parameters. * parameters.
* *
* @param expression must not be {@literal null}. * @param expression must not be {@literal null}.
* @param operation must not be {@literal null}. * @param operation must not be {@literal null}.
* @param parameters * @param parameters
@ -321,7 +339,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* A {@link Projection} based on a SpEL expression. * A {@link Projection} based on a SpEL expression.
* *
* @author Thomas Darimont * @author Thomas Darimont
* @author Oliver Gierke * @author Oliver Gierke
*/ */
@ -334,7 +352,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Creates a new {@link ExpressionProjection} for the given field, SpEL expression and parameters. * Creates a new {@link ExpressionProjection} for the given field, SpEL expression and parameters.
* *
* @param field must not be {@literal null}. * @param field must not be {@literal null}.
* @param expression must not be {@literal null} or empty. * @param expression must not be {@literal null} or empty.
* @param parameters must not be {@literal null}. * @param parameters must not be {@literal null}.
@ -350,7 +368,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
this.params = parameters.clone(); this.params = parameters.clone();
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.ProjectionOperation.Projection#toDBObject(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext) * @see org.springframework.data.mongodb.core.aggregation.ProjectionOperation.Projection#toDBObject(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext)
*/ */
@ -368,7 +386,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Builder for {@link ProjectionOperation}s on a field. * Builder for {@link ProjectionOperation}s on a field.
* *
* @author Oliver Gierke * @author Oliver Gierke
* @author Thomas Darimont * @author Thomas Darimont
* @author Christoph Strobl * @author Christoph Strobl
@ -384,7 +402,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Creates a new {@link ProjectionOperationBuilder} for the field with the given name on top of the given * Creates a new {@link ProjectionOperationBuilder} for the field with the given name on top of the given
* {@link ProjectionOperation}. * {@link ProjectionOperation}.
* *
* @param name must not be {@literal null} or empty. * @param name must not be {@literal null} or empty.
* @param operation must not be {@literal null}. * @param operation must not be {@literal null}.
* @param previousProjection the previous operation projection, may be {@literal null}. * @param previousProjection the previous operation projection, may be {@literal null}.
@ -400,7 +418,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Creates a new {@link ProjectionOperationBuilder} for the field with the given value on top of the given * Creates a new {@link ProjectionOperationBuilder} for the field with the given value on top of the given
* {@link ProjectionOperation}. * {@link ProjectionOperation}.
* *
* @param value * @param value
* @param operation * @param operation
* @param previousProjection * @param previousProjection
@ -417,7 +435,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Projects the result of the previous operation onto the current field. Will automatically add an exclusion for * Projects the result of the previous operation onto the current field. Will automatically add an exclusion for
* {@code _id} as what would be held in it by default will now go into the field just projected into. * {@code _id} as what would be held in it by default will now go into the field just projected into.
* *
* @return * @return
*/ */
public ProjectionOperation previousOperation() { public ProjectionOperation previousOperation() {
@ -428,7 +446,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Defines a nested field binding for the current field. * Defines a nested field binding for the current field.
* *
* @param fields must not be {@literal null}. * @param fields must not be {@literal null}.
* @return * @return
*/ */
@ -438,7 +456,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Allows to specify an alias for the previous projection operation. * Allows to specify an alias for the previous projection operation.
* *
* @param alias * @param alias
* @return * @return
*/ */
@ -480,7 +498,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Generates an {@code $add} expression that adds the given number to the previously mentioned field. * Generates an {@code $add} expression that adds the given number to the previously mentioned field.
* *
* @param number * @param number
* @return * @return
*/ */
@ -492,7 +510,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Generates an {@code $add} expression that adds the value of the given field to the previously mentioned field. * Generates an {@code $add} expression that adds the value of the given field to the previously mentioned field.
* *
* @param fieldReference * @param fieldReference
* @return * @return
*/ */
@ -504,7 +522,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Generates an {@code $subtract} expression that subtracts the given number to the previously mentioned field. * Generates an {@code $subtract} expression that subtracts the given number to the previously mentioned field.
* *
* @param number * @param number
* @return * @return
*/ */
@ -517,7 +535,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Generates an {@code $subtract} expression that subtracts the value of the given field to the previously mentioned * Generates an {@code $subtract} expression that subtracts the value of the given field to the previously mentioned
* field. * field.
* *
* @param fieldReference * @param fieldReference
* @return * @return
*/ */
@ -543,7 +561,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Generates an {@code $multiply} expression that multiplies the given number with the previously mentioned field. * Generates an {@code $multiply} expression that multiplies the given number with the previously mentioned field.
* *
* @param number * @param number
* @return * @return
*/ */
@ -556,7 +574,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Generates an {@code $multiply} expression that multiplies the value of the given field with the previously * Generates an {@code $multiply} expression that multiplies the value of the given field with the previously
* mentioned field. * mentioned field.
* *
* @param fieldReference * @param fieldReference
* @return * @return
*/ */
@ -582,7 +600,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Generates an {@code $divide} expression that divides the previously mentioned field by the given number. * Generates an {@code $divide} expression that divides the previously mentioned field by the given number.
* *
* @param number * @param number
* @return * @return
*/ */
@ -596,7 +614,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Generates an {@code $divide} expression that divides the value of the given field by the previously mentioned * Generates an {@code $divide} expression that divides the value of the given field by the previously mentioned
* field. * field.
* *
* @param fieldReference * @param fieldReference
* @return * @return
*/ */
@ -623,7 +641,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Generates an {@code $mod} expression that divides the previously mentioned field by the given number and returns * Generates an {@code $mod} expression that divides the previously mentioned field by the given number and returns
* the remainder. * the remainder.
* *
* @param number * @param number
* @return * @return
*/ */
@ -1232,7 +1250,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Adds a generic projection for the current field. * Adds a generic projection for the current field.
* *
* @param operation the operation key, e.g. {@code $add}. * @param operation the operation key, e.g. {@code $add}.
* @param values the values to be set for the projection operation. * @param values the values to be set for the projection operation.
* @return * @return
@ -1245,7 +1263,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* A {@link Projection} to pull in the result of the previous operation. * A {@link Projection} to pull in the result of the previous operation.
* *
* @author Oliver Gierke * @author Oliver Gierke
*/ */
static class PreviousOperationProjection extends Projection { static class PreviousOperationProjection extends Projection {
@ -1254,7 +1272,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Creates a new {@link PreviousOperationProjection} for the field with the given name. * Creates a new {@link PreviousOperationProjection} for the field with the given name.
* *
* @param name must not be {@literal null} or empty. * @param name must not be {@literal null} or empty.
*/ */
public PreviousOperationProjection(String name) { public PreviousOperationProjection(String name) {
@ -1262,7 +1280,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
this.name = name; this.name = name;
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.ProjectionOperation.Projection#toDBObject(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext) * @see org.springframework.data.mongodb.core.aggregation.ProjectionOperation.Projection#toDBObject(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext)
*/ */
@ -1274,7 +1292,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* A {@link FieldProjection} to map a result of a previous {@link AggregationOperation} to a new field. * A {@link FieldProjection} to map a result of a previous {@link AggregationOperation} to a new field.
* *
* @author Oliver Gierke * @author Oliver Gierke
* @author Thomas Darimont * @author Thomas Darimont
* @author Mark Paluch * @author Mark Paluch
@ -1286,7 +1304,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Creates a new {@link FieldProjection} for the field of the given name, assigning the given value. * Creates a new {@link FieldProjection} for the field of the given name, assigning the given value.
* *
* @param name must not be {@literal null} or empty. * @param name must not be {@literal null} or empty.
* @param value * @param value
*/ */
@ -1305,7 +1323,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Factory method to easily create {@link FieldProjection}s for the given {@link Fields}. Fields are projected as * Factory method to easily create {@link FieldProjection}s for the given {@link Fields}. Fields are projected as
* references with their given name. A field {@code foo} will be projected as: {@code foo : 1 } . * references with their given name. A field {@code foo} will be projected as: {@code foo : 1 } .
* *
* @param fields the {@link Fields} to in- or exclude, must not be {@literal null}. * @param fields the {@link Fields} to in- or exclude, must not be {@literal null}.
* @return * @return
*/ */
@ -1315,7 +1333,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Factory method to easily create {@link FieldProjection}s for the given {@link Fields}. * Factory method to easily create {@link FieldProjection}s for the given {@link Fields}.
* *
* @param fields the {@link Fields} to in- or exclude, must not be {@literal null}. * @param fields the {@link Fields} to in- or exclude, must not be {@literal null}.
* @param value to use for the given field. * @param value to use for the given field.
* @return * @return
@ -1332,7 +1350,14 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
return projections; return projections;
} }
/* /**
* @return {@literal true} if this field is excluded.
*/
public boolean isExcluded() {
return Boolean.FALSE.equals(value);
}
/*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.ProjectionOperation.Projection#toDBObject(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext) * @see org.springframework.data.mongodb.core.aggregation.ProjectionOperation.Projection#toDBObject(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext)
*/ */
@ -1371,7 +1396,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Creates a new {@link OperationProjection} for the given field. * Creates a new {@link OperationProjection} for the given field.
* *
* @param field the name of the field to add the operation projection for, must not be {@literal null} or empty. * @param field the name of the field to add the operation projection for, must not be {@literal null} or empty.
* @param operation the actual operation key, must not be {@literal null} or empty. * @param operation the actual operation key, must not be {@literal null} or empty.
* @param values the values to pass into the operation, must not be {@literal null}. * @param values the values to pass into the operation, must not be {@literal null}.
@ -1425,7 +1450,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Returns the field that holds the {@link OperationProjection}. * Returns the field that holds the {@link OperationProjection}.
* *
* @return * @return
*/ */
protected Field getField() { protected Field getField() {
@ -1448,7 +1473,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Creates a new instance of this {@link OperationProjection} with the given alias. * Creates a new instance of this {@link OperationProjection} with the given alias.
* *
* @param alias the alias to set * @param alias the alias to set
* @return * @return
*/ */
@ -1489,7 +1514,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
this.fields = fields; this.fields = fields;
} }
/* /*
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.ProjectionOperation.Projection#toDBObject(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext) * @see org.springframework.data.mongodb.core.aggregation.ProjectionOperation.Projection#toDBObject(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext)
*/ */
@ -1508,7 +1533,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Extracts the minute from a date expression. * Extracts the minute from a date expression.
* *
* @return * @return
*/ */
public ProjectionOperationBuilder extractMinute() { public ProjectionOperationBuilder extractMinute() {
@ -1517,7 +1542,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Extracts the hour from a date expression. * Extracts the hour from a date expression.
* *
* @return * @return
*/ */
public ProjectionOperationBuilder extractHour() { public ProjectionOperationBuilder extractHour() {
@ -1526,7 +1551,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Extracts the second from a date expression. * Extracts the second from a date expression.
* *
* @return * @return
*/ */
public ProjectionOperationBuilder extractSecond() { public ProjectionOperationBuilder extractSecond() {
@ -1535,7 +1560,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Extracts the millisecond from a date expression. * Extracts the millisecond from a date expression.
* *
* @return * @return
*/ */
public ProjectionOperationBuilder extractMillisecond() { public ProjectionOperationBuilder extractMillisecond() {
@ -1544,7 +1569,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Extracts the year from a date expression. * Extracts the year from a date expression.
* *
* @return * @return
*/ */
public ProjectionOperationBuilder extractYear() { public ProjectionOperationBuilder extractYear() {
@ -1553,7 +1578,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Extracts the month from a date expression. * Extracts the month from a date expression.
* *
* @return * @return
*/ */
public ProjectionOperationBuilder extractMonth() { public ProjectionOperationBuilder extractMonth() {
@ -1562,7 +1587,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Extracts the week from a date expression. * Extracts the week from a date expression.
* *
* @return * @return
*/ */
public ProjectionOperationBuilder extractWeek() { public ProjectionOperationBuilder extractWeek() {
@ -1571,7 +1596,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Extracts the dayOfYear from a date expression. * Extracts the dayOfYear from a date expression.
* *
* @return * @return
*/ */
public ProjectionOperationBuilder extractDayOfYear() { public ProjectionOperationBuilder extractDayOfYear() {
@ -1580,7 +1605,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Extracts the dayOfMonth from a date expression. * Extracts the dayOfMonth from a date expression.
* *
* @return * @return
*/ */
public ProjectionOperationBuilder extractDayOfMonth() { public ProjectionOperationBuilder extractDayOfMonth() {
@ -1589,7 +1614,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Extracts the dayOfWeek from a date expression. * Extracts the dayOfWeek from a date expression.
* *
* @return * @return
*/ */
public ProjectionOperationBuilder extractDayOfWeek() { public ProjectionOperationBuilder extractDayOfWeek() {
@ -1599,7 +1624,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Base class for {@link Projection} implementations. * Base class for {@link Projection} implementations.
* *
* @author Oliver Gierke * @author Oliver Gierke
*/ */
private static abstract class Projection { private static abstract class Projection {
@ -1608,7 +1633,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Creates new {@link Projection} for the given {@link Field}. * Creates new {@link Projection} for the given {@link Field}.
* *
* @param field must not be {@literal null}. * @param field must not be {@literal null}.
*/ */
public Projection(Field field) { public Projection(Field field) {
@ -1619,7 +1644,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Returns the field exposed by the {@link Projection}. * Returns the field exposed by the {@link Projection}.
* *
* @return will never be {@literal null}. * @return will never be {@literal null}.
*/ */
public ExposedField getExposedField() { public ExposedField getExposedField() {
@ -1629,7 +1654,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* Renders the current {@link Projection} into a {@link DBObject} based on the given * Renders the current {@link Projection} into a {@link DBObject} based on the given
* {@link AggregationOperationContext}. * {@link AggregationOperationContext}.
* *
* @param context will never be {@literal null}. * @param context will never be {@literal null}.
* @return * @return
*/ */
@ -1639,14 +1664,14 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
/** /**
* @author Thomas Darimont * @author Thomas Darimont
*/ */
static class ExpressionProjection extends Projection { static class ExpressionProjection extends ProjectionOperation.Projection {
private final AggregationExpression expression; private final AggregationExpression expression;
private final Field field; private final Field field;
/** /**
* Creates a new {@link ExpressionProjection}. * Creates a new {@link ExpressionProjection}.
* *
* @param field * @param field
* @param expression * @param expression
*/ */

10
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ReplaceRootOperation.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2016-2017 the original author or authors. * Copyright 2016-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -95,6 +95,14 @@ public class ReplaceRootOperation implements FieldsExposingAggregationOperation
return ExposedFields.from(); return ExposedFields.from();
} }
/* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.FieldsExposingAggregationOperation#inheritsFields()
*/
@Override
public boolean inheritsFields() {
return false;
}
/** /**
* Builder for {@link ReplaceRootOperation}. * Builder for {@link ReplaceRootOperation}.
* *

10
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/UnwindOperation.java

@ -26,7 +26,7 @@ import com.mongodb.DBObject;
* <p> * <p>
* We recommend to use the static factory method {@link Aggregation#unwind(String)} instead of creating instances of * We recommend to use the static factory method {@link Aggregation#unwind(String)} instead of creating instances of
* this class directly. * this class directly.
* *
* @author Thomas Darimont * @author Thomas Darimont
* @author Oliver Gierke * @author Oliver Gierke
* @author Mark Paluch * @author Mark Paluch
@ -117,6 +117,14 @@ public class UnwindOperation
return arrayIndex != null ? ExposedFields.from(arrayIndex) : ExposedFields.from(); return arrayIndex != null ? ExposedFields.from(arrayIndex) : ExposedFields.from();
} }
/* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.FieldsExposingAggregationOperation#inheritsFields()
*/
@Override
public boolean inheritsFields() {
return true;
}
/** /**
* Get a builder that allows creation of {@link LookupOperation}. * Get a builder that allows creation of {@link LookupOperation}.
* *

48
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperationUnitTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2013-2017 the original author or authors. * Copyright 2013-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -211,9 +211,19 @@ public class ProjectionOperationUnitTests {
public void excludeShouldAllowExclusionOfFieldsOtherThanUnderscoreId/* since MongoDB 3.4 */() { public void excludeShouldAllowExclusionOfFieldsOtherThanUnderscoreId/* since MongoDB 3.4 */() {
ProjectionOperation projectionOp = new ProjectionOperation().andExclude("foo"); ProjectionOperation projectionOp = new ProjectionOperation().andExclude("foo");
Document document = projectionOp.toDocument(Aggregation.DEFAULT_CONTEXT); DBObject document = projectionOp.toDBObject(Aggregation.DEFAULT_CONTEXT);
Document projectClause = DocumentTestUtils.getAsDocument(document, PROJECT); DBObject projectClause = DBObjectTestUtils.getAsDBObject(document, PROJECT);
assertThat((Integer) projectClause.get("foo")).isEqualTo(0);
assertThat(projectionOp.inheritsFields(), is(true));
assertThat((Integer) projectClause.get("foo"), is(0));
}
@Test // DATAMONGO-1893
public void includeShouldNotInheritFields() {
ProjectionOperation projectionOp = new ProjectionOperation().andInclude("foo");
assertThat(projectionOp.inheritsFields(), is(false));
} }
@Test // DATAMONGO-758 @Test // DATAMONGO-758
@ -1548,14 +1558,13 @@ public class ProjectionOperationUnitTests {
.andApply(AggregationFunctionExpressions.MULTIPLY.of(Fields.field("total"), Fields.field("discounted")))) // .andApply(AggregationFunctionExpressions.MULTIPLY.of(Fields.field("total"), Fields.field("discounted")))) //
.as("finalTotal").toDBObject(Aggregation.DEFAULT_CONTEXT); .as("finalTotal").toDBObject(Aggregation.DEFAULT_CONTEXT);
assertThat(agg, assertThat(agg, is(JSON.parse("{ $project:{ \"finalTotal\" : { \"$let\": {" + //
is(JSON.parse("{ $project:{ \"finalTotal\" : { \"$let\": {" + // "\"vars\": {" + //
"\"vars\": {" + // "\"total\": { \"$add\": [ \"$price\", \"$tax\" ] }," + //
"\"total\": { \"$add\": [ \"$price\", \"$tax\" ] }," + // "\"discounted\": { \"$cond\": { \"if\": \"$applyDiscount\", \"then\": 0.9, \"else\": 1.0 } }" + //
"\"discounted\": { \"$cond\": { \"if\": \"$applyDiscount\", \"then\": 0.9, \"else\": 1.0 } }" + // "}," + //
"}," + // "\"in\": { \"$multiply\": [ \"$$total\", \"$$discounted\" ] }" + //
"\"in\": { \"$multiply\": [ \"$$total\", \"$$discounted\" ] }" + // "}}}}")));
"}}}}")));
} }
@Test // DATAMONGO-1538 @Test // DATAMONGO-1538
@ -1572,14 +1581,13 @@ public class ProjectionOperationUnitTests {
AggregationFunctionExpressions.MULTIPLY.of(Fields.field("total"), Fields.field("discounted"))) AggregationFunctionExpressions.MULTIPLY.of(Fields.field("total"), Fields.field("discounted")))
.as("finalTotal").toDBObject(Aggregation.DEFAULT_CONTEXT); .as("finalTotal").toDBObject(Aggregation.DEFAULT_CONTEXT);
assertThat(agg, assertThat(agg, is(JSON.parse("{ $project:{ \"finalTotal\" : { \"$let\": {" + //
is(JSON.parse("{ $project:{ \"finalTotal\" : { \"$let\": {" + // "\"vars\": {" + //
"\"vars\": {" + // "\"total\": { \"$add\": [ \"$price\", \"$tax\" ] }," + //
"\"total\": { \"$add\": [ \"$price\", \"$tax\" ] }," + // "\"discounted\": { \"$cond\": { \"if\": \"$applyDiscount\", \"then\": 0.9, \"else\": 1.0 } }" + //
"\"discounted\": { \"$cond\": { \"if\": \"$applyDiscount\", \"then\": 0.9, \"else\": 1.0 } }" + // "}," + //
"}," + // "\"in\": { \"$multiply\": [ \"$$total\", \"$$discounted\" ] }" + //
"\"in\": { \"$multiply\": [ \"$$total\", \"$$discounted\" ] }" + // "}}}}")));
"}}}}")));
} }
@Test // DATAMONGO-1548 @Test // DATAMONGO-1548

29
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/TypeBasedAggregationOperationContextUnitTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2013-2017 the original author or authors. * Copyright 2013-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,6 +22,8 @@ import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
import static org.springframework.data.mongodb.core.aggregation.Fields.*; import static org.springframework.data.mongodb.core.aggregation.Fields.*;
import static org.springframework.data.mongodb.test.util.IsBsonObject.*; import static org.springframework.data.mongodb.test.util.IsBsonObject.*;
import lombok.AllArgsConstructor;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -34,7 +36,7 @@ import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.support.GenericConversionService; import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceConstructor; import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction; import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mapping.model.MappingException; import org.springframework.data.mapping.model.MappingException;
import org.springframework.data.mongodb.core.aggregation.ExposedFields.DirectFieldReference; import org.springframework.data.mongodb.core.aggregation.ExposedFields.DirectFieldReference;
@ -196,6 +198,20 @@ public class TypeBasedAggregationOperationContextUnitTests {
.containing("age", "$age.value")); .containing("age", "$age.value"));
} }
@Test // DATAMONGO-1893
public void considersIncludedFieldsFromSingleExclusionsCorrectly() {
AggregationOperationContext context = getContext(FooPerson.class);
TypedAggregation<FooPerson> agg = newAggregation(FooPerson.class, project() //
.andExclude("name"), sort(new Sort("age.value", "lastName")));
DBObject dbo = agg.toDbObject("person", context);
DBObject sort = getPipelineElementFromAggregationAt(dbo, 1);
assertThat(getAsDBObject(sort, "$sort"),
is(equalTo((DBObject) new BasicDBObject("age.value", 1).append("last_name", 1))));
}
@Test // DATAMONGO-1133 @Test // DATAMONGO-1133
public void shouldHonorAliasedFieldsInGroupExpressions() { public void shouldHonorAliasedFieldsInGroupExpressions() {
@ -352,18 +368,13 @@ public class TypeBasedAggregationOperationContextUnitTests {
} }
@Document(collection = "person") @Document(collection = "person")
@AllArgsConstructor
public static class FooPerson { public static class FooPerson {
final ObjectId id; final ObjectId id;
final String name; final String name;
@org.springframework.data.mongodb.core.mapping.Field("last_name") final String lastName;
final Age age; final Age age;
@PersistenceConstructor
FooPerson(ObjectId id, String name, Age age) {
this.id = id;
this.name = name;
this.age = age;
}
} }
public static class Age { public static class Age {

Loading…
Cancel
Save