Browse Source
Added Zipcode sample dataset from 10gen. Allow Projections to be used in conjunction with GroupOperations. Integration & Refactoring of github contribution by Tobias Trelle and Sebastian Herold. Switched from builder-style to static factory based DSL construction of aggregation specifications. Introduced embedded DSL for convenient construction of aggregation specifications. Added test cases based on mongodb aggregation framework examples. Added more test cases, additional java doc. Added test case for unwind operation (returnFiveMostCommonLikes) in AggregationTests. Other test cases should now also run in CI environment, due to deterministic result ordering. Adjusted write concern to ensure persistence of sample data. Introduced TypedAggregation which holds type information of the input type of an aggregation. Cleaned up aggregate methods on MongoOperations. Removed HasToDBObject interface. Cleaned up constructors for Aggregation and TypedAggregation.pull/58/merge
30 changed files with 31029 additions and 437 deletions
@ -0,0 +1,62 @@
@@ -0,0 +1,62 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
import com.mongodb.BasicDBObject; |
||||
import com.mongodb.DBObject; |
||||
|
||||
/** |
||||
* @author Thomas Darimont |
||||
*/ |
||||
abstract class AbstractAggregateOperation implements AggregationOperation { |
||||
private final String operationName; |
||||
|
||||
/** |
||||
* @param operationName |
||||
*/ |
||||
public AbstractAggregateOperation(String operationName) { |
||||
this.operationName = operationName; |
||||
} |
||||
|
||||
/** |
||||
* @return the operationName |
||||
*/ |
||||
public String getOperationName() { |
||||
return operationName; |
||||
} |
||||
|
||||
public String getOperationCommand() { |
||||
return OPERATOR_PREFIX + getOperationName(); |
||||
} |
||||
|
||||
/** |
||||
* @return the argument for the operation |
||||
*/ |
||||
public abstract Object getOperationArgument(); |
||||
|
||||
@Override |
||||
public DBObject toDbObject() { |
||||
return new BasicDBObject(getOperationCommand(), getOperationArgument()); |
||||
} |
||||
|
||||
/* (non-Javadoc) |
||||
* @see java.lang.Object#toString() |
||||
*/ |
||||
@Override |
||||
public String toString() { |
||||
return String.valueOf(toDbObject()); |
||||
} |
||||
} |
||||
@ -1,211 +0,0 @@
@@ -1,211 +0,0 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import org.springframework.data.domain.Sort; |
||||
import org.springframework.data.mongodb.core.query.Criteria; |
||||
import org.springframework.util.Assert; |
||||
|
||||
import com.mongodb.BasicDBObject; |
||||
import com.mongodb.DBObject; |
||||
import com.mongodb.util.JSON; |
||||
import com.mongodb.util.JSONParseException; |
||||
|
||||
/** |
||||
* Holds the operations of an aggregation pipeline. |
||||
* |
||||
* @author Tobias Trelle |
||||
* @since 1.3 |
||||
*/ |
||||
public class AggregationPipeline { |
||||
|
||||
private static final String OPERATOR_PREFIX = "$"; |
||||
|
||||
private final List<DBObject> operations = new ArrayList<DBObject>(); |
||||
|
||||
public AggregationPipeline() { |
||||
} |
||||
|
||||
/** |
||||
* Creates a new {@link AggregationPipeline} from the given {@link AggregationOperation}s. |
||||
* |
||||
* @param operations must not be {@literal null} or empty. |
||||
*/ |
||||
public AggregationPipeline(AggregationOperation... operations) { |
||||
|
||||
Assert.notNull(operations, "Operations must not be null!"); |
||||
Assert.isTrue(operations.length > 0, "operations must not be empty!"); |
||||
|
||||
for (AggregationOperation operation : operations) { |
||||
Assert.notNull(operation, "Operation is not allowed to be null"); |
||||
this.operations.add(operation.getDBObject()); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Adds a projection operation to the pipeline. |
||||
* |
||||
* @param projection JSON string holding the projection, must not be {@literal null} or empty. |
||||
* @return The pipeline. |
||||
*/ |
||||
public AggregationPipeline project(String projection) { |
||||
return addDocumentOperation("project", projection); |
||||
} |
||||
|
||||
/** |
||||
* Adds a projection operation to the pipeline. |
||||
* |
||||
* @param projection Type safe projection object, must not be {@literal null}. |
||||
* @return The pipeline. |
||||
*/ |
||||
public AggregationPipeline project(Projection projection) { |
||||
|
||||
Assert.notNull(projection, "Projection must not be null!"); |
||||
return addOperation("project", projection.toDBObject()); |
||||
} |
||||
|
||||
/** |
||||
* Adds an unwind operation to the pipeline. |
||||
* |
||||
* @param field Name of the field to unwind (should be an array), must not be {@literal null} or empty. |
||||
* @return The pipeline. |
||||
*/ |
||||
public AggregationPipeline unwind(String field) { |
||||
|
||||
Assert.hasText(field, "Missing field name"); |
||||
|
||||
if (!field.startsWith(OPERATOR_PREFIX)) { |
||||
field = OPERATOR_PREFIX + field; |
||||
} |
||||
|
||||
return addOperation("unwind", field); |
||||
} |
||||
|
||||
/** |
||||
* Adds a group operation to the pipeline. |
||||
* |
||||
* @param group JSON string holding the group, must not be {@literal null} or empty. |
||||
* @return The pipeline. |
||||
*/ |
||||
public AggregationPipeline group(String group) { |
||||
return addDocumentOperation("group", group); |
||||
} |
||||
|
||||
/** |
||||
* Adds a sort operation to the pipeline. |
||||
* |
||||
* @param sort JSON string holding the sorting, must not be {@literal null} or empty. |
||||
* @return The pipeline. |
||||
*/ |
||||
public AggregationPipeline sort(String sort) { |
||||
return addDocumentOperation("sort", sort); |
||||
} |
||||
|
||||
/** |
||||
* Adds a sort operation to the pipeline. |
||||
* |
||||
* @param sort Type safe sort operation, must not be {@literal null}. |
||||
* @return The pipeline. |
||||
*/ |
||||
public AggregationPipeline sort(Sort sort) { |
||||
|
||||
Assert.notNull(sort); |
||||
DBObject dbo = new BasicDBObject(); |
||||
|
||||
for (org.springframework.data.domain.Sort.Order order : sort) { |
||||
dbo.put(order.getProperty(), order.isAscending() ? 1 : -1); |
||||
} |
||||
return addOperation("sort", dbo); |
||||
} |
||||
|
||||
/** |
||||
* Adds a match operation to the pipeline that is basically a query on the collections. |
||||
* |
||||
* @param match JSON string holding the criteria, must not be {@literal null} or empty. |
||||
* @return The pipeline. |
||||
*/ |
||||
public AggregationPipeline match(String match) { |
||||
return addDocumentOperation("match", match); |
||||
} |
||||
|
||||
/** |
||||
* Adds a match operation to the pipeline that is basically a query on the collection.s |
||||
* |
||||
* @param criteria Type safe criteria to filter documents from the collection, must not be {@literal null}. |
||||
* @return The pipeline. |
||||
*/ |
||||
public AggregationPipeline match(Criteria criteria) { |
||||
|
||||
Assert.notNull(criteria); |
||||
return addOperation("match", criteria.getCriteriaObject()); |
||||
} |
||||
|
||||
/** |
||||
* Adds an limit operation to the pipeline. |
||||
* |
||||
* @param n Number of document to consider. |
||||
* @return The pipeline. |
||||
*/ |
||||
public AggregationPipeline limit(long n) { |
||||
return addOperation("limit", n); |
||||
} |
||||
|
||||
/** |
||||
* Adds an skip operation to the pipeline. |
||||
* |
||||
* @param n Number of documents to skip. |
||||
* @return The pipeline. |
||||
*/ |
||||
public AggregationPipeline skip(long n) { |
||||
return addOperation("skip", n); |
||||
} |
||||
|
||||
public List<DBObject> getOperations() { |
||||
return operations; |
||||
} |
||||
|
||||
/** |
||||
* creates an empty pipeline |
||||
* |
||||
* @return the new pipeline |
||||
*/ |
||||
public static AggregationPipeline pipeline() { |
||||
return new AggregationPipeline(); |
||||
} |
||||
|
||||
private AggregationPipeline addDocumentOperation(String opName, String operation) { |
||||
|
||||
Assert.hasText(operation, "Operation must not be null or empty!"); |
||||
return addOperation(opName, parseJson(operation)); |
||||
} |
||||
|
||||
private AggregationPipeline addOperation(String key, Object value) { |
||||
this.operations.add(new BasicDBObject(OPERATOR_PREFIX + key, value)); |
||||
return this; |
||||
} |
||||
|
||||
private DBObject parseJson(String json) { |
||||
|
||||
try { |
||||
return (DBObject) JSON.parse(json); |
||||
} catch (JSONParseException e) { |
||||
throw new IllegalArgumentException("Invalid JSON document: " + json, e); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,48 @@
@@ -0,0 +1,48 @@
|
||||
package org.springframework.data.mongodb.core.aggregation; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* Implementation of {@link Fields} |
||||
* |
||||
* @author Thomas Darimont |
||||
*/ |
||||
class BackendFields implements Fields { |
||||
|
||||
private Map<String, Object> values = new HashMap<String, Object>(); |
||||
|
||||
/** |
||||
* @param names |
||||
*/ |
||||
public BackendFields(String... names) { |
||||
for (String name : names) { |
||||
and(name); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* @param name |
||||
* @return |
||||
*/ |
||||
public Fields and(String name) { |
||||
return and(name, ReferenceUtil.safeReference(name)); |
||||
} |
||||
|
||||
/** |
||||
* @param name |
||||
* @param value |
||||
* @return |
||||
*/ |
||||
public Fields and(String name, Object value) { |
||||
this.values.put(name, value); |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* @return |
||||
*/ |
||||
public Map<String, Object> getValues() { |
||||
return new HashMap<String, Object>(this.values); |
||||
} |
||||
} |
||||
@ -0,0 +1,31 @@
@@ -0,0 +1,31 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* Fields is a collection of key-value pairs. |
||||
* |
||||
* @author Thomas Darimont |
||||
*/ |
||||
public interface Fields { |
||||
Map<String, Object> getValues(); |
||||
|
||||
Fields and(String name); |
||||
|
||||
Fields and(String name, Object value); |
||||
} |
||||
@ -0,0 +1,42 @@
@@ -0,0 +1,42 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
import org.springframework.data.mongodb.core.query.NearQuery; |
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* @author Thomas Darimont |
||||
*/ |
||||
public class GeoNearOperation extends AbstractAggregateOperation { |
||||
|
||||
private final NearQuery nearQuery; |
||||
|
||||
public GeoNearOperation(NearQuery nearQuery) { |
||||
super("geoNear"); |
||||
Assert.notNull(nearQuery); |
||||
this.nearQuery = nearQuery; |
||||
} |
||||
|
||||
/* (non-Javadoc) |
||||
* @see org.springframework.data.mongodb.core.aggregation.AbstractAggregateOperation#getOperationArgument() |
||||
*/ |
||||
@Override |
||||
public Object getOperationArgument() { |
||||
return nearQuery.toDBObject(); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,40 @@
@@ -0,0 +1,40 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
/** |
||||
* @author Thomas Darimont |
||||
*/ |
||||
class LimitOperation extends AbstractAggregateOperation { |
||||
|
||||
private final long maxElements; |
||||
|
||||
/** |
||||
* @param maxElements Number of documents to consider. |
||||
*/ |
||||
public LimitOperation(long maxElements) { |
||||
super("limit"); |
||||
this.maxElements = maxElements; |
||||
} |
||||
|
||||
/* (non-Javadoc) |
||||
* @see org.springframework.data.mongodb.core.aggregation.AbstractAggregateOperation#getOperationArgument() |
||||
*/ |
||||
@Override |
||||
public Object getOperationArgument() { |
||||
return maxElements; |
||||
} |
||||
} |
||||
@ -0,0 +1,28 @@
@@ -0,0 +1,28 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
import com.mongodb.BasicDBObject; |
||||
import com.mongodb.DBObject; |
||||
|
||||
/** |
||||
* @author Thomas Darimont |
||||
*/ |
||||
public class NoopAggreationOperation implements AggregationOperation { |
||||
public DBObject toDbObject() { |
||||
return new BasicDBObject(); |
||||
} |
||||
} |
||||
@ -0,0 +1,40 @@
@@ -0,0 +1,40 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
/** |
||||
* @author Thomas Darimont |
||||
*/ |
||||
public class SkipOperation extends AbstractAggregateOperation { |
||||
|
||||
private final long skipCount; |
||||
|
||||
/** |
||||
* @param skipCount number of documents to skip. |
||||
*/ |
||||
public SkipOperation(long skipCount) { |
||||
super("skip"); |
||||
this.skipCount = skipCount; |
||||
} |
||||
|
||||
/* (non-Javadoc) |
||||
* @see org.springframework.data.mongodb.core.aggregation.AbstractAggregateOperation#getOperationArgument() |
||||
*/ |
||||
@Override |
||||
public Object getOperationArgument() { |
||||
return skipCount; |
||||
} |
||||
} |
||||
@ -0,0 +1,67 @@
@@ -0,0 +1,67 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
import org.springframework.data.domain.Sort; |
||||
import org.springframework.util.Assert; |
||||
|
||||
import com.mongodb.BasicDBObject; |
||||
import com.mongodb.DBObject; |
||||
|
||||
/** |
||||
* @author Thomas Darimont |
||||
*/ |
||||
public class SortOperation extends AbstractAggregateOperation { |
||||
|
||||
private Sort sort; |
||||
|
||||
/** |
||||
* @param sort |
||||
*/ |
||||
public SortOperation(Sort sort) { |
||||
super("sort"); |
||||
|
||||
Assert.notNull(sort); |
||||
this.sort = sort; |
||||
} |
||||
|
||||
public SortOperation and(Sort sort) { |
||||
return new SortOperation(this.sort.and(sort)); |
||||
} |
||||
|
||||
public SortOperation and(Sort.Direction direction, String... fields) { |
||||
return and(new Sort(direction, fields)); |
||||
} |
||||
|
||||
/* (non-Javadoc) |
||||
* @see org.springframework.data.mongodb.core.aggregation.AbstractAggregateOperation#getOperationArgument() |
||||
*/ |
||||
@Override |
||||
public Object getOperationArgument() { |
||||
return createSortProperties(); |
||||
} |
||||
|
||||
/** |
||||
* @return |
||||
*/ |
||||
private DBObject createSortProperties() { |
||||
DBObject sortProperties = new BasicDBObject(); |
||||
for (org.springframework.data.domain.Sort.Order order : sort) { |
||||
sortProperties.put(order.getProperty(), order.isAscending() ? 1 : -1); |
||||
} |
||||
return sortProperties; |
||||
} |
||||
} |
||||
@ -0,0 +1,44 @@
@@ -0,0 +1,44 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
/** |
||||
* A {@code TypedAggregation} is a special {@link Aggregation} that holds information of the input aggregation type. |
||||
* |
||||
* @author Thomas Darimont |
||||
*/ |
||||
public class TypedAggregation<I, O> extends Aggregation<I, O> { |
||||
|
||||
private Class<I> inputType; |
||||
|
||||
/** |
||||
* Creates a new {@link TypedAggregation} from the given {@link AggregationOperation}s. |
||||
* |
||||
* @param operations must not be {@literal null} or empty. |
||||
*/ |
||||
public TypedAggregation(Class<I> inputType, AggregationOperation... operations) { |
||||
super(operations); |
||||
this.inputType = inputType; |
||||
} |
||||
|
||||
/** |
||||
* @return the inputType |
||||
*/ |
||||
public Class<?> getInputType() { |
||||
return inputType; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,37 @@
@@ -0,0 +1,37 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* @author Thomas Darimont |
||||
*/ |
||||
public class UnwindOperation extends AbstractAggregateOperation { |
||||
|
||||
private final String fieldName; |
||||
|
||||
public UnwindOperation(String fieldName) { |
||||
super("unwind"); |
||||
Assert.notNull(fieldName); |
||||
this.fieldName = fieldName; |
||||
} |
||||
|
||||
@Override |
||||
public Object getOperationArgument() { |
||||
return ReferenceUtil.safeReference(fieldName); |
||||
} |
||||
} |
||||
@ -0,0 +1,19 @@
@@ -0,0 +1,19 @@
|
||||
/* |
||||
* 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. |
||||
*/ |
||||
/** |
||||
* @author Thomas Darimont |
||||
*/ |
||||
package org.springframework.data.mongodb.core.aggregation; |
||||
@ -0,0 +1,11 @@
@@ -0,0 +1,11 @@
|
||||
package org.springframework.data.mongodb.core.aggregation; |
||||
|
||||
class City { |
||||
|
||||
String name; |
||||
int population; |
||||
|
||||
public String toString() { |
||||
return "City [name=" + name + ", population=" + population + "]"; |
||||
} |
||||
} |
||||
@ -0,0 +1,27 @@
@@ -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; |
||||
|
||||
import org.springframework.data.mongodb.core.mapping.Field; |
||||
|
||||
/** |
||||
* @author Thomas Darimont |
||||
*/ |
||||
public class LikeStats { |
||||
|
||||
String id; |
||||
@Field("number") long count; |
||||
} |
||||
@ -0,0 +1,28 @@
@@ -0,0 +1,28 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
import org.springframework.data.annotation.Id; |
||||
import org.springframework.data.mongodb.core.mapping.Field; |
||||
|
||||
/** |
||||
* @author Thomas Darimont |
||||
*/ |
||||
class StateStats { |
||||
@Id String id; |
||||
String state; |
||||
@Field("totalPop") int totalPopulation; |
||||
} |
||||
@ -0,0 +1,37 @@
@@ -0,0 +1,37 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
import java.util.Arrays; |
||||
import java.util.Date; |
||||
import java.util.HashSet; |
||||
import java.util.Set; |
||||
|
||||
/** |
||||
* @author Thomas Darimont |
||||
*/ |
||||
public class UserWithLikes { |
||||
|
||||
String id; |
||||
Date joined; |
||||
Set<String> likes = new HashSet<String>(); |
||||
|
||||
public UserWithLikes(String id, Date joined, String... likes) { |
||||
this.id = id; |
||||
this.joined = joined; |
||||
this.likes = new HashSet<String>(Arrays.asList(likes)); |
||||
} |
||||
} |
||||
@ -0,0 +1,25 @@
@@ -0,0 +1,25 @@
|
||||
package org.springframework.data.mongodb.core.aggregation; |
||||
|
||||
import java.util.Arrays; |
||||
|
||||
import org.springframework.data.mongodb.core.mapping.Field; |
||||
|
||||
/** |
||||
* Data model from mongodb reference data set |
||||
* |
||||
* @see http://docs.mongodb.org/manual/tutorial/aggregation-examples/
|
||||
* @see http://media.mongodb.org/zips.json
|
||||
*/ |
||||
class ZipInfo { |
||||
|
||||
String id; |
||||
String city; |
||||
String state; |
||||
@Field("pop") int population; |
||||
@Field("loc") double[] location; |
||||
|
||||
public String toString() { |
||||
return "ZipInfo [id=" + id + ", city=" + city + ", state=" + state + ", population=" + population + ", location=" |
||||
+ Arrays.toString(location) + "]"; |
||||
} |
||||
} |
||||
@ -0,0 +1,14 @@
@@ -0,0 +1,14 @@
|
||||
package org.springframework.data.mongodb.core.aggregation; |
||||
|
||||
class ZipInfoStats { |
||||
|
||||
String id; |
||||
String state; |
||||
City biggestCity; |
||||
City smallestCity; |
||||
|
||||
public String toString() { |
||||
return "ZipInfoStats [id=" + id + ", state=" + state + ", biggestCity=" + biggestCity + ", smallestCity=" |
||||
+ smallestCity + "]"; |
||||
} |
||||
} |
||||
Loading…
Reference in new issue