Skip to content

DATAMONGO-1326 - Add support for $lookup to aggregation. #344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core.aggregation;

/**
* {@link AggregationOperation} that exposes <b>additional</b> {@link ExposedFields} that can be used for later
* aggregation pipeline {@code AggregationOperation}s, e.g. lookup operation produces a field which has to be added to
* the current ones.
*
* @author Alessio Fachechi
*/
public interface AdditionalFieldsExposingAggregationOperation extends FieldsExposingAggregationOperation {

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@
/**
* An {@code Aggregation} is a representation of a list of aggregation steps to be performed by the MongoDB Aggregation
* Framework.
*
*
* @author Tobias Trelle
* @author Thomas Darimont
* @author Oliver Gierke
* @author Alessio Fachechi
* @since 1.3
*/
public class Aggregation {
Expand All @@ -65,7 +66,7 @@ public class Aggregation {

/**
* Creates a new {@link Aggregation} from the given {@link AggregationOperation}s.
*
*
* @param operations must not be {@literal null} or empty.
*/
public static Aggregation newAggregation(List<? extends AggregationOperation> operations) {
Expand All @@ -74,7 +75,7 @@ public static Aggregation newAggregation(List<? extends AggregationOperation> op

/**
* Creates a new {@link Aggregation} from the given {@link AggregationOperation}s.
*
*
* @param operations must not be {@literal null} or empty.
*/
public static Aggregation newAggregation(AggregationOperation... operations) {
Expand All @@ -84,7 +85,7 @@ public static Aggregation newAggregation(AggregationOperation... operations) {
/**
* Returns a copy of this {@link Aggregation} with the given {@link AggregationOptions} set. Note that options are
* supported in MongoDB version 2.6+.
*
*
* @param options must not be {@literal null}.
* @return
* @since 1.6
Expand All @@ -97,7 +98,7 @@ public Aggregation withOptions(AggregationOptions options) {

/**
* Creates a new {@link TypedAggregation} for the given type and {@link AggregationOperation}s.
*
*
* @param type must not be {@literal null}.
* @param operations must not be {@literal null} or empty.
*/
Expand All @@ -107,7 +108,7 @@ public static <T> TypedAggregation<T> newAggregation(Class<T> type, List<? exten

/**
* Creates a new {@link TypedAggregation} for the given type and {@link AggregationOperation}s.
*
*
* @param type must not be {@literal null}.
* @param operations must not be {@literal null} or empty.
*/
Expand All @@ -117,7 +118,7 @@ public static <T> TypedAggregation<T> newAggregation(Class<T> type, AggregationO

/**
* Creates a new {@link Aggregation} from the given {@link AggregationOperation}s.
*
*
* @param aggregationOperations must not be {@literal null} or empty.
*/
protected Aggregation(AggregationOperation... aggregationOperations) {
Expand All @@ -137,7 +138,7 @@ protected static List<AggregationOperation> asAggregationList(AggregationOperati

/**
* Creates a new {@link Aggregation} from the given {@link AggregationOperation}s.
*
*
* @param aggregationOperations must not be {@literal null} or empty.
*/
protected Aggregation(List<AggregationOperation> aggregationOperations) {
Expand All @@ -146,7 +147,7 @@ protected Aggregation(List<AggregationOperation> aggregationOperations) {

/**
* Creates a new {@link Aggregation} from the given {@link AggregationOperation}s.
*
*
* @param aggregationOperations must not be {@literal null} or empty.
* @param options must not be {@literal null} or empty.
*/
Expand All @@ -162,7 +163,7 @@ protected Aggregation(List<AggregationOperation> aggregationOperations, Aggregat

/**
* A pointer to the previous {@link AggregationOperation}.
*
*
* @return
*/
public static String previousOperation() {
Expand All @@ -171,7 +172,7 @@ public static String previousOperation() {

/**
* Creates a new {@link ProjectionOperation} including the given fields.
*
*
* @param fields must not be {@literal null}.
* @return
*/
Expand All @@ -181,7 +182,7 @@ public static ProjectionOperation project(String... fields) {

/**
* Creates a new {@link ProjectionOperation} includeing the given {@link Fields}.
*
*
* @param fields must not be {@literal null}.
* @return
*/
Expand All @@ -191,7 +192,7 @@ public static ProjectionOperation project(Fields fields) {

/**
* Factory method to create a new {@link UnwindOperation} for the field with the given name.
*
*
* @param fieldName must not be {@literal null} or empty.
* @return
*/
Expand All @@ -201,7 +202,7 @@ public static UnwindOperation unwind(String field) {

/**
* Creates a new {@link GroupOperation} for the given fields.
*
*
* @param fields must not be {@literal null}.
* @return
*/
Expand All @@ -211,7 +212,7 @@ public static GroupOperation group(String... fields) {

/**
* Creates a new {@link GroupOperation} for the given {@link Fields}.
*
*
* @param fields must not be {@literal null}.
* @return
*/
Expand All @@ -221,7 +222,7 @@ public static GroupOperation group(Fields fields) {

/**
* Factory method to create a new {@link SortOperation} for the given {@link Sort}.
*
*
* @param sort must not be {@literal null}.
* @return
*/
Expand All @@ -231,7 +232,7 @@ public static SortOperation sort(Sort sort) {

/**
* Factory method to create a new {@link SortOperation} for the given sort {@link Direction} and {@code fields}.
*
*
* @param direction must not be {@literal null}.
* @param fields must not be {@literal null}.
* @return
Expand All @@ -242,7 +243,7 @@ public static SortOperation sort(Direction direction, String... fields) {

/**
* Creates a new {@link SkipOperation} skipping the given number of elements.
*
*
* @param elementsToSkip must not be less than zero.
* @return
*/
Expand All @@ -252,7 +253,7 @@ public static SkipOperation skip(int elementsToSkip) {

/**
* Creates a new {@link LimitOperation} limiting the result to the given number of elements.
*
*
* @param maxElements must not be less than zero.
* @return
*/
Expand All @@ -262,28 +263,48 @@ public static LimitOperation limit(long maxElements) {

/**
* Creates a new {@link MatchOperation} using the given {@link Criteria}.
*
*
* @param criteria must not be {@literal null}.
* @return
*/
public static MatchOperation match(Criteria criteria) {
return new MatchOperation(criteria);
}

/**
* Creates a new {@link LookupOperation} for the given fields.
*
* @param fields must not be {@literal null}.
* @return
*/
public static LookupOperation lookup(String from, String localField, String foreignField, String as) {
return lookup(field(from), field(localField), field(foreignField), field(as));
}

/**
* Creates a new {@link LookupOperation} for the given {@link Fields}.
*
* @param fields must not be {@literal null}.
* @return
*/
public static LookupOperation lookup(Field from, Field localField, Field foreignField, Field as) {
return new LookupOperation(from, localField, foreignField, as);
}

/**
* Creates a new {@link Fields} instance for the given field names.
*
* @see Fields#fields(String...)
*
* @param fields must not be {@literal null}.
* @return
* @see Fields#fields(String...)
*/
public static Fields fields(String... fields) {
return Fields.fields(fields);
}

/**
* Creates a new {@link Fields} instance from the given field name and target reference.
*
*
* @param name must not be {@literal null} or empty.
* @param target must not be {@literal null} or empty.
* @return
Expand All @@ -295,7 +316,7 @@ public static Fields bind(String name, String target) {
/**
* Creates a new {@link GeoNearOperation} instance from the given {@link NearQuery} and the{@code distanceField}. The
* {@code distanceField} defines output field that contains the calculated distance.
*
*
* @param query must not be {@literal null}.
* @param distanceField must not be {@literal null} or empty.
* @return
Expand All @@ -307,7 +328,7 @@ public static GeoNearOperation geoNear(NearQuery query, String distanceField) {

/**
* Returns a new {@link AggregationOptions.Builder}.
*
*
* @return
* @since 1.6
*/
Expand All @@ -317,7 +338,7 @@ public static AggregationOptions.Builder newAggregationOptions() {

/**
* Converts this {@link Aggregation} specification to a {@link DBObject}.
*
*
* @param inputCollectionName the name of the input collection
* @return the {@code DBObject} representing this aggregation
*/
Expand All @@ -331,8 +352,11 @@ public DBObject toDbObject(String inputCollectionName, AggregationOperationConte
operationDocuments.add(operation.toDBObject(context));

if (operation instanceof FieldsExposingAggregationOperation) {
boolean additional = operation instanceof AdditionalFieldsExposingAggregationOperation ? true : false;

FieldsExposingAggregationOperation exposedFieldsOperation = (FieldsExposingAggregationOperation) operation;
context = new ExposedFieldsAggregationOperationContext(exposedFieldsOperation.getFields(), rootContext);
context = new ExposedFieldsAggregationOperationContext(exposedFieldsOperation.getFields(), rootContext,
additional);
}
}

Expand All @@ -356,7 +380,7 @@ public String toString() {

/**
* Simple {@link AggregationOperationContext} that just returns {@link FieldReference}s as is.
*
*
* @author Oliver Gierke
*/
private static class NoOpAggregationOperationContext implements AggregationOperationContext {
Expand Down Expand Up @@ -391,7 +415,7 @@ public FieldReference getReference(String name) {

/**
* Describes the system variables available in MongoDB aggregation framework pipeline expressions.
*
*
* @author Thomas Darimont
* @see http://docs.mongodb.org/manual/reference/aggregation-variables
*/
Expand All @@ -404,7 +428,7 @@ enum SystemVariable {
/**
* Return {@literal true} if the given {@code fieldRef} denotes a well-known system variable, {@literal false}
* otherwise.
*
*
* @param fieldRef may be {@literal null}.
* @return
*/
Expand Down
Loading