diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ConvertOperators.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ConvertOperators.java new file mode 100644 index 000000000..1fbc641b0 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ConvertOperators.java @@ -0,0 +1,667 @@ +/* + * Copyright 2018 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.Collections; + +import org.springframework.util.Assert; + +/** + * Gateway to {@literal convert} aggregation operations. + * + * @author Christoph Strobl + * @since 1.10.15 + */ +public class ConvertOperators { + + /** + * Take the value referenced by given {@literal fieldReference}. + * + * @param fieldReference must not be {@literal null}. + * @return + */ + public static ConvertOperatorFactory valueOf(String fieldReference) { + return new ConvertOperatorFactory(fieldReference); + } + + /** + * Take the value provided by the given {@link AggregationExpression}. + * + * @param expression must not be {@literal null}. + * @return + */ + public static ConvertOperatorFactory valueOf(AggregationExpression expression) { + return new ConvertOperatorFactory(expression); + } + + /** + * @author Christoph Strobl + */ + public static class ConvertOperatorFactory { + + private final String fieldReference; + private final AggregationExpression expression; + + /** + * Creates new {@link ConvertOperatorFactory} for given {@literal fieldReference}. + * + * @param fieldReference must not be {@literal null}. + */ + public ConvertOperatorFactory(String fieldReference) { + + Assert.notNull(fieldReference, "FieldReference must not be null!"); + this.fieldReference = fieldReference; + this.expression = null; + } + + /** + * Creates new {@link ConvertOperatorFactory} for given {@link AggregationExpression}. + * + * @param expression must not be {@literal null}. + */ + public ConvertOperatorFactory(AggregationExpression expression) { + + Assert.notNull(expression, "Expression must not be null!"); + this.fieldReference = null; + this.expression = expression; + } + + /** + * Creates new {@link Convert aggregation expression} that takes the associated value and converts it into the type + * specified by the given identifier.
+ * NOTE: Requires MongoDB 4.0 or later. + * + * @param stringTypeIdentifier must not be {@literal null}. + * @return new instance of {@link Convert}. + */ + public Convert convertTo(String stringTypeIdentifier) { + return createConvert().to(stringTypeIdentifier); + } + + /** + * Creates new {@link Convert aggregation expression} that takes the associated value and converts it into the type + * specified by the given identifier.
+ * NOTE: Requires MongoDB 4.0 or later. + * + * @param numericTypeIdentifier must not be {@literal null}. + * @return new instance of {@link Convert}. + */ + public Convert convertTo(int numericTypeIdentifier) { + return createConvert().to(numericTypeIdentifier); + } + + /** + * Creates new {@link Convert aggregation expression} that takes the associated value and converts it into the type + * specified by the value of the given {@link Field field reference}.
+ * NOTE: Requires MongoDB 4.0 or later. + * + * @param fieldReference must not be {@literal null}. + * @return new instance of {@link Convert}. + */ + public Convert convertToTypeOf(String fieldReference) { + return createConvert().toTypeOf(fieldReference); + } + + /** + * Creates new {@link Convert aggregation expression} that takes the associated value and converts it into the type + * specified by the given {@link AggregationExpression expression}.
+ * NOTE: Requires MongoDB 4.0 or later. + * + * @param expression must not be {@literal null}. + * @return new instance of {@link Convert}. + */ + public Convert convertToTypeOf(AggregationExpression expression) { + return createConvert().toTypeOf(expression); + } + + /** + * Creates new {@link ToBool aggregation expression} for {@code $toBool} that converts a value to boolean. Shorthand + * for {@link #convertTo(String) #convertTo("bool")}.
+ * NOTE: Requires MongoDB 4.0 or later. + * + * @return new instance of {@link ToBool}. + */ + public ToBool convertToBoolean() { + return ToBool.toBoolean(valueObject()); + } + + /** + * Creates new {@link ToDate aggregation expression} for {@code $toDate} that converts a value to a date. Shorthand + * for {@link #convertTo(String) #convertTo("date")}.
+ * NOTE: Requires MongoDB 4.0 or later. + * + * @return new instance of {@link ToDate}. + */ + public ToDate convertToDate() { + return ToDate.toDate(valueObject()); + } + + /** + * Creates new {@link ToDecimal aggregation expression} for {@code $toDecimal} that converts a value to a decimal. + * Shorthand for {@link #convertTo(String) #convertTo("decimal")}.
+ * NOTE: Requires MongoDB 4.0 or later. + * + * @return new instance of {@link ToDecimal}. + */ + public ToDecimal convertToDecimal() { + return ToDecimal.toDecimal(valueObject()); + } + + /** + * Creates new {@link ToDouble aggregation expression} for {@code $toDouble} that converts a value to a decimal. + * Shorthand for {@link #convertTo(String) #convertTo("double")}.
+ * NOTE: Requires MongoDB 4.0 or later. + * + * @return new instance of {@link ToDouble}. + */ + public ToDouble convertToDouble() { + return ToDouble.toDouble(valueObject()); + } + + /** + * Creates new {@link ToInt aggregation expression} for {@code $toInt} that converts a value to an int. Shorthand + * for {@link #convertTo(String) #convertTo("int")}.
+ * NOTE: Requires MongoDB 4.0 or later. + * + * @return new instance of {@link ToInt}. + */ + public ToInt convertToInt() { + return ToInt.toInt(valueObject()); + } + + /** + * Creates new {@link ToInt aggregation expression} for {@code $toLong} that converts a value to a long. Shorthand + * for {@link #convertTo(String) #convertTo("long")}.
+ * NOTE: Requires MongoDB 4.0 or later. + * + * @return new instance of {@link ToInt}. + */ + public ToLong convertToLong() { + return ToLong.toLong(valueObject()); + } + + /** + * Creates new {@link ToInt aggregation expression} for {@code $toObjectId} that converts a value to a objectId. Shorthand + * for {@link #convertTo(String) #convertTo("objectId")}.
+ * NOTE: Requires MongoDB 4.0 or later. + * + * @return new instance of {@link ToInt}. + */ + public ToObjectId convertToObjectId() { + return ToObjectId.toObjectId(valueObject()); + } + + /** + * Creates new {@link ToInt aggregation expression} for {@code $toString} that converts a value to a string. Shorthand + * for {@link #convertTo(String) #convertTo("string")}.
+ * NOTE: Requires MongoDB 4.0 or later. + * + * @return new instance of {@link ToInt}. + */ + public ToString convertToString() { + return ToString.toString(valueObject()); + } + + private Convert createConvert() { + return usesFieldRef() ? Convert.convertValueOf(fieldReference) : Convert.convertValueOf(expression); + } + + private Object valueObject() { + return usesFieldRef() ? Fields.field(fieldReference) : expression; + } + + private boolean usesFieldRef() { + return fieldReference != null; + } + } + + /** + * {@link AggregationExpression} for {@code $convert} that converts a value to a specified type.
+ * NOTE: Requires MongoDB 4.0 or later. + * + * @author Christoph Strobl + * @see https://docs.mongodb.com/manual/reference/operator/aggregation/convert/ + * @since 1.10.15 + */ + public static class Convert extends AbstractAggregationExpression { + + private Convert(Object value) { + super(value); + } + + /** + * Creates new {@link Convert} using the given value for the {@literal input} attribute. + * + * @param value must not be {@literal null}. + * @return new instance of {@link Convert}. + */ + public static Convert convertValue(Object value) { + return new Convert(Collections.singletonMap("input", value)); + } + + /** + * Creates new {@link Convert} using the value of the provided {@link Field fieldReference} as {@literal input} + * value. + * + * @param fieldReference must not be {@literal null}. + * @return new instance of {@link Convert}. + */ + public static Convert convertValueOf(String fieldReference) { + return convertValue(Fields.field(fieldReference)); + } + + /** + * Creates new {@link Convert} using the result of the provided {@link AggregationExpression expression} as + * {@literal input} value. + * + * @param expression must not be {@literal null}. + * @return new instance of {@link Convert}. + */ + public static Convert convertValueOf(AggregationExpression expression) { + return convertValue(expression); + } + + /** + * Specify the conversion target type via its {@link String} representation. + * + * + * @param stringTypeIdentifier must not be {@literal null}. + * @return new instance of {@link Convert}. + */ + public Convert to(String stringTypeIdentifier) { + return new Convert(append("to", stringTypeIdentifier)); + } + + /** + * Specify the conversion target type via its numeric representation. + *
+ *
1
+ *
double
+ *
2
+ *
string + *
7
+ *
objectId + *
8
+ *
bool
+ *
9
+ *
date
+ *
16
+ *
int
+ *
18
+ *
long
+ *
19
+ *
decimal
+ *
+ * + * @param numericTypeIdentifier must not be {@literal null}. + * @return new instance of {@link Convert}. + */ + public Convert to(int numericTypeIdentifier) { + return new Convert(append("to", numericTypeIdentifier)); + } + + /** + * Specify the conversion target type via the value of the given field. + * + * @param fieldReference must not be {@literal null}. + * @return new instance of {@link Convert}. + */ + public Convert toTypeOf(String fieldReference) { + return new Convert(append("to", Fields.field(fieldReference))); + } + + /** + * Specify the conversion target type via the value of the given {@link AggregationExpression expression}. + * + * @param expression must not be {@literal null}. + * @return new instance of {@link Convert}. + */ + public Convert toTypeOf(AggregationExpression expression) { + return new Convert(append("to", expression)); + } + + /** + * Optionally specify the value to return on encountering an error during conversion. + * + * @param value must not be {@literal null}. + * @return new instance of {@link Convert}. + */ + public Convert onErrorReturn(Object value) { + return new Convert(append("onError", value)); + } + + /** + * Optionally specify the field holding the value to return on encountering an error during conversion. + * + * @param fieldReference must not be {@literal null}. + * @return new instance of {@link Convert}. + */ + public Convert onErrorReturnValueOf(String fieldReference) { + return onErrorReturn(Fields.field(fieldReference)); + } + + /** + * Optionally specify the expression to evaluate and return on encountering an error during conversion. + * + * @param expression must not be {@literal null}. + * @return new instance of {@link Convert}. + */ + public Convert onErrorReturnValueOf(AggregationExpression expression) { + return onErrorReturn(expression); + } + + /** + * Optionally specify the value to return when the input is {@literal null} or missing. + * + * @param value must not be {@literal null}. + * @return new instance of {@link Convert}. + */ + public Convert onNullReturn(Object value) { + return new Convert(append("onNull", value)); + } + + /** + * Optionally specify the field holding the value to return when the input is {@literal null} or missing. + * + * @param fieldReference must not be {@literal null}. + * @return new instance of {@link Convert}. + */ + public Convert onNullReturnValueOf(String fieldReference) { + return onNullReturn(Fields.field(fieldReference)); + } + + /** + * Optionally specify the expression to evaluate and return when the input is {@literal null} or missing. + * + * @param expression must not be {@literal null}. + * @return new instance of {@link Convert}. + */ + public Convert onNullReturnValueOf(AggregationExpression expression) { + return onNullReturn(expression); + } + + @Override + protected String getMongoMethod() { + return "$convert"; + } + } + + /** + * {@link AggregationExpression} for {@code $toBool} that converts a value to boolean. Shorthand for + * {@link Convert#to(String) Convert#to("bool")}.
+ * NOTE: Requires MongoDB 4.0 or later. + * + * @author Christoph Strobl + * @see https://docs.mongodb.com/manual/reference/operator/aggregation/toBool/ + * @since 1.10.15 + */ + public static class ToBool extends AbstractAggregationExpression { + + private ToBool(Object value) { + super(value); + } + + /** + * Creates new {@link ToBool} using the given value as input. + * + * @param value must not be {@literal null}. + * @return new instance of {@link ToBool}. + */ + public static ToBool toBoolean(Object value) { + return new ToBool(value); + } + + @Override + protected String getMongoMethod() { + return "$toBool"; + } + } + + /** + * {@link AggregationExpression} for {@code $toDate} that converts a value to boolean. Shorthand for + * {@link Convert#to(String) Convert#to("date")}.
+ * NOTE: Requires MongoDB 4.0 or later. + * + * @author Christoph Strobl + * @see https://docs.mongodb.com/manual/reference/operator/aggregation/toDate/ + * @since 1.10.15 + */ + public static class ToDate extends AbstractAggregationExpression { + + private ToDate(Object value) { + super(value); + } + + /** + * Creates new {@link ToDate} using the given value as input. + * + * @param value must not be {@literal null}. + * @return new instance of {@link ToDate}. + */ + public static ToDate toDate(Object value) { + return new ToDate(value); + } + + @Override + protected String getMongoMethod() { + return "$toDate"; + } + } + + /** + * {@link AggregationExpression} for {@code $toDecimal} that converts a value to decimal. Shorthand for + * {@link Convert#to(String) Convert#to("decimal")}.
+ * NOTE: Requires MongoDB 4.0 or later. + * + * @author Christoph Strobl + * @see https://docs.mongodb.com/manual/reference/operator/aggregation/toDecimal/ + * @since 1.10.15 + */ + public static class ToDecimal extends AbstractAggregationExpression { + + private ToDecimal(Object value) { + super(value); + } + + /** + * Creates new {@link ToDecimal} using the given value as input. + * + * @param value must not be {@literal null}. + * @return new instance of {@link ToDecimal}. + */ + public static ToDecimal toDecimal(Object value) { + return new ToDecimal(value); + } + + @Override + protected String getMongoMethod() { + return "$toDecimal"; + } + } + + /** + * {@link AggregationExpression} for {@code $toDouble} that converts a value to double. Shorthand for + * {@link Convert#to(String) Convert#to("double")}.
+ * NOTE: Requires MongoDB 4.0 or later. + * + * @author Christoph Strobl + * @see https://docs.mongodb.com/manual/reference/operator/aggregation/toDouble/ + * @since 1.10.15 + */ + public static class ToDouble extends AbstractAggregationExpression { + + private ToDouble(Object value) { + super(value); + } + + /** + * Creates new {@link ToDouble} using the given value as input. + * + * @param value must not be {@literal null}. + * @return new instance of {@link ToDouble}. + */ + public static ToDouble toDouble(Object value) { + return new ToDouble(value); + } + + @Override + protected String getMongoMethod() { + return "$toDouble"; + } + } + + /** + * {@link AggregationExpression} for {@code $toInt} that converts a value to integer. Shorthand for + * {@link Convert#to(String) Convert#to("int")}.
+ * NOTE: Requires MongoDB 4.0 or later. + * + * @author Christoph Strobl + * @see https://docs.mongodb.com/manual/reference/operator/aggregation/toInt/ + * @since 1.10.15 + */ + public static class ToInt extends AbstractAggregationExpression { + + private ToInt(Object value) { + super(value); + } + + /** + * Creates new {@link ToInt} using the given value as input. + * + * @param value must not be {@literal null}. + * @return new instance of {@link ToInt}. + */ + public static ToInt toInt(Object value) { + return new ToInt(value); + } + + @Override + protected String getMongoMethod() { + return "$toInt"; + } + } + + /** + * {@link AggregationExpression} for {@code $toLong} that converts a value to long. Shorthand for + * {@link Convert#to(String) Convert#to("long")}.
+ * NOTE: Requires MongoDB 4.0 or later. + * + * @author Christoph Strobl + * @see https://docs.mongodb.com/manual/reference/operator/aggregation/toLong/ + * @since 1.10.15 + */ + public static class ToLong extends AbstractAggregationExpression { + + private ToLong(Object value) { + super(value); + } + + /** + * Creates new {@link ToLong} using the given value as input. + * + * @param value must not be {@literal null}. + * @return new instance of {@link ToLong}. + */ + public static ToLong toLong(Object value) { + return new ToLong(value); + } + + @Override + protected String getMongoMethod() { + return "$toLong"; + } + } + + /** + * {@link AggregationExpression} for {@code $toObjectId} that converts a value to objectId. Shorthand for + * {@link Convert#to(String) Convert#to("objectId")}.
+ * NOTE: Requires MongoDB 4.0 or later. + * + * @author Christoph Strobl + * @see https://docs.mongodb.com/manual/reference/operator/aggregation/toObjectId/ + * @since 1.10.15 + */ + public static class ToObjectId extends AbstractAggregationExpression { + + private ToObjectId(Object value) { + super(value); + } + + /** + * Creates new {@link ToObjectId} using the given value as input. + * + * @param value must not be {@literal null}. + * @return new instance of {@link ToObjectId}. + */ + public static ToObjectId toObjectId(Object value) { + return new ToObjectId(value); + } + + @Override + protected String getMongoMethod() { + return "$toObjectId"; + } + } + + /** + * {@link AggregationExpression} for {@code $toString} that converts a value to string. Shorthand for + * {@link Convert#to(String) Convert#to("string")}.
+ * NOTE: Requires MongoDB 4.0 or later. + * + * @author Christoph Strobl + * @see https://docs.mongodb.com/manual/reference/operator/aggregation/toString/ + * @since 1.10.15 + */ + public static class ToString extends AbstractAggregationExpression { + + private ToString(Object value) { + super(value); + } + + /** + * Creates new {@link ToString} using the given value as input. + * + * @param value must not be {@literal null}. + * @return new instance of {@link ToString}. + */ + public static ToString toString(Object value) { + return new ToString(value); + } + + @Override + protected String getMongoMethod() { + return "$toString"; + } + } +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ConvertOperatorsUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ConvertOperatorsUnitTests.java new file mode 100644 index 000000000..b795b81fa --- /dev/null +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ConvertOperatorsUnitTests.java @@ -0,0 +1,232 @@ +/* + * Copyright 2018 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 static org.hamcrest.MatcherAssert.*; +import static org.hamcrest.Matchers.*; + +import org.junit.Test; + +import com.mongodb.DBObject; +import com.mongodb.util.JSON; + +/** + * Unit tests for {@link ConvertOperators}. + * + * @author Christoph Strobl + * @currentRead Royal Assassin - Robin Hobb + */ +public class ConvertOperatorsUnitTests { + + static final String EXPRESSION_STRING = "{ \"$molly\" : \"chandler\" }"; + static final DBObject EXPRESSION_DOC = (DBObject) JSON.parse(EXPRESSION_STRING); + static final AggregationExpression EXPRESSION = new AggregationExpression() { + @Override + public DBObject toDbObject(AggregationOperationContext context) { + return EXPRESSION_DOC; + } + }; + + @Test // DATAMONGO-2048 + public void convertToUsingStringIdentifier() { + + assertThat(ConvertOperators.valueOf("shrewd").convertTo("double").toDbObject(Aggregation.DEFAULT_CONTEXT), + is(JSON.parse("{ $convert: { \"input\" : \"$shrewd\", \"to\" : \"double\" } } "))); + } + + @Test // DATAMONGO-2048 + public void convertToUsingIntIdentifier() { + + assertThat(ConvertOperators.valueOf("shrewd").convertTo(1).toDbObject(Aggregation.DEFAULT_CONTEXT), + is(JSON.parse("{ $convert: { \"input\" : \"$shrewd\", \"to\" : 1 } } "))); + } + + @Test // DATAMONGO-2048 + public void convertToUsingFieldReference() { + + assertThat(ConvertOperators.valueOf("shrewd").convertToTypeOf("fitz").toDbObject(Aggregation.DEFAULT_CONTEXT), + is(JSON.parse("{ $convert: { \"input\" : \"$shrewd\", \"to\" : \"$fitz\" } } "))); + } + + @Test // DATAMONGO-2048 + public void convertToUsingExpression() { + + assertThat(ConvertOperators.valueOf("shrewd").convertToTypeOf(EXPRESSION).toDbObject(Aggregation.DEFAULT_CONTEXT), + is(JSON.parse("{ $convert: { \"input\" : \"$shrewd\", \"to\" : " + EXPRESSION_STRING + " } } "))); + } + + @Test // DATAMONGO-2048 + public void convertToWithOnErrorValue() { + + assertThat(ConvertOperators.valueOf("shrewd").convertTo("double").onErrorReturn("foo") + .toDbObject(Aggregation.DEFAULT_CONTEXT), + is(JSON.parse("{ $convert: { \"input\" : \"$shrewd\", \"to\" : \"double\", \"onError\" : \"foo\" } } "))); + } + + @Test // DATAMONGO-2048 + public void convertToWithOnErrorValueOfField() { + + assertThat(ConvertOperators.valueOf("shrewd").convertTo("double").onErrorReturnValueOf("verity") + .toDbObject(Aggregation.DEFAULT_CONTEXT), + is(JSON.parse("{ $convert: { \"input\" : \"$shrewd\", \"to\" : \"double\", \"onError\" : \"$verity\" } } "))); + } + + @Test // DATAMONGO-2048 + public void convertToWithOnErrorValueOfExpression() { + + assertThat(ConvertOperators.valueOf("shrewd").convertTo("double").onErrorReturnValueOf(EXPRESSION) + .toDbObject(Aggregation.DEFAULT_CONTEXT), + is(JSON.parse("{ $convert: { \"input\" : \"$shrewd\", \"to\" : \"double\", \"onError\" : " + EXPRESSION_STRING + + " } } "))); + } + + @Test // DATAMONGO-2048 + public void convertToWithOnNullValue() { + + assertThat(ConvertOperators.valueOf("shrewd").convertTo("double").onNullReturn("foo") + .toDbObject(Aggregation.DEFAULT_CONTEXT), + is(JSON.parse("{ $convert: { \"input\" : \"$shrewd\", \"to\" : \"double\", \"onNull\" : \"foo\" } } "))); + } + + @Test // DATAMONGO-2048 + public void convertToWithOnNullValueOfField() { + + assertThat(ConvertOperators.valueOf("shrewd").convertTo("double").onNullReturnValueOf("verity") + .toDbObject(Aggregation.DEFAULT_CONTEXT), + is(JSON.parse("{ $convert: { \"input\" : \"$shrewd\", \"to\" : \"double\", \"onNull\" : \"$verity\" } } "))); + } + + @Test // DATAMONGO-2048 + public void convertToWithOnNullValueOfExpression() { + + assertThat(ConvertOperators.valueOf("shrewd").convertTo("double").onNullReturnValueOf(EXPRESSION) + .toDbObject(Aggregation.DEFAULT_CONTEXT), + is(JSON.parse("{ $convert: { \"input\" : \"$shrewd\", \"to\" : \"double\", \"onNull\" : " + EXPRESSION_STRING + + " } } "))); + } + + @Test // DATAMONGO-2048 + public void toBoolUsingFieldReference() { + + assertThat(ConvertOperators.valueOf("shrewd").convertToBoolean().toDbObject(Aggregation.DEFAULT_CONTEXT), + is(JSON.parse("{ $toBool: \"$shrewd\" } "))); + } + + @Test // DATAMONGO-2048 + public void toBoolUsingExpression() { + + assertThat(ConvertOperators.valueOf(EXPRESSION).convertToBoolean().toDbObject(Aggregation.DEFAULT_CONTEXT), + is(JSON.parse("{ $toBool: " + EXPRESSION_STRING + " } "))); + } + + @Test // DATAMONGO-2048 + public void toDateUsingFieldReference() { + + assertThat(ConvertOperators.valueOf("shrewd").convertToDate().toDbObject(Aggregation.DEFAULT_CONTEXT), + is(JSON.parse("{ $toDate: \"$shrewd\" } "))); + } + + @Test // DATAMONGO-2048 + public void toDateUsingExpression() { + + assertThat(ConvertOperators.valueOf(EXPRESSION).convertToDate().toDbObject(Aggregation.DEFAULT_CONTEXT), + is(JSON.parse("{ $toDate: " + EXPRESSION_STRING + " } "))); + } + + @Test // DATAMONGO-2048 + public void toDecimalUsingFieldReference() { + + assertThat(ConvertOperators.valueOf("shrewd").convertToDecimal().toDbObject(Aggregation.DEFAULT_CONTEXT), + is(JSON.parse("{ $toDecimal: \"$shrewd\" } "))); + } + + @Test // DATAMONGO-2048 + public void toDecimalUsingExpression() { + + assertThat(ConvertOperators.valueOf(EXPRESSION).convertToDecimal().toDbObject(Aggregation.DEFAULT_CONTEXT), + is(JSON.parse("{ $toDecimal: " + EXPRESSION_STRING + " } "))); + } + + @Test // DATAMONGO-2048 + public void toDoubleUsingFieldReference() { + + assertThat(ConvertOperators.valueOf("shrewd").convertToDouble().toDbObject(Aggregation.DEFAULT_CONTEXT), + is(JSON.parse("{ $toDouble: \"$shrewd\" } "))); + } + + @Test // DATAMONGO-2048 + public void toDoubleUsingExpression() { + + assertThat(ConvertOperators.valueOf(EXPRESSION).convertToDouble().toDbObject(Aggregation.DEFAULT_CONTEXT), + is(JSON.parse("{ $toDouble: " + EXPRESSION_STRING + " } "))); + } + + @Test // DATAMONGO-2048 + public void toIntUsingFieldReference() { + + assertThat(ConvertOperators.valueOf("shrewd").convertToInt().toDbObject(Aggregation.DEFAULT_CONTEXT), + is(JSON.parse("{ $toInt: \"$shrewd\" } "))); + } + + @Test // DATAMONGO-2048 + public void toIntUsingExpression() { + + assertThat(ConvertOperators.valueOf(EXPRESSION).convertToInt().toDbObject(Aggregation.DEFAULT_CONTEXT), + is(JSON.parse("{ $toInt: " + EXPRESSION_STRING + " } "))); + } + + @Test // DATAMONGO-2048 + public void toLongUsingFieldReference() { + + assertThat(ConvertOperators.valueOf("shrewd").convertToLong().toDbObject(Aggregation.DEFAULT_CONTEXT), + is(JSON.parse("{ $toLong: \"$shrewd\" } "))); + } + + @Test // DATAMONGO-2048 + public void toLongUsingExpression() { + + assertThat(ConvertOperators.valueOf(EXPRESSION).convertToLong().toDbObject(Aggregation.DEFAULT_CONTEXT), + is(JSON.parse("{ $toLong: " + EXPRESSION_STRING + " } "))); + } + + @Test // DATAMONGO-2048 + public void toObjectIdUsingFieldReference() { + + assertThat(ConvertOperators.valueOf("shrewd").convertToObjectId().toDbObject(Aggregation.DEFAULT_CONTEXT), + is(JSON.parse("{ $toObjectId: \"$shrewd\" } "))); + } + + @Test // DATAMONGO-2048 + public void toObjectIdUsingExpression() { + + assertThat(ConvertOperators.valueOf(EXPRESSION).convertToObjectId().toDbObject(Aggregation.DEFAULT_CONTEXT), + is(JSON.parse("{ $toObjectId: " + EXPRESSION_STRING + " } "))); + } + + @Test // DATAMONGO-2048 + public void toStringUsingFieldReference() { + + assertThat(ConvertOperators.valueOf("shrewd").convertToString().toDbObject(Aggregation.DEFAULT_CONTEXT), + is(JSON.parse("{ $toString: \"$shrewd\" } "))); + } + + @Test // DATAMONGO-2048 + public void toStringUsingExpression() { + + assertThat(ConvertOperators.valueOf(EXPRESSION).convertToString().toDbObject(Aggregation.DEFAULT_CONTEXT), + is(JSON.parse("{ $toString: " + EXPRESSION_STRING + " } "))); + } +} diff --git a/src/main/asciidoc/reference/mongodb.adoc b/src/main/asciidoc/reference/mongodb.adoc index 66acc0029..1f924a459 100644 --- a/src/main/asciidoc/reference/mongodb.adoc +++ b/src/main/asciidoc/reference/mongodb.adoc @@ -1690,6 +1690,7 @@ The MongoDB Aggregation Framework provides the following types of aggregation op * Array Aggregation Operators * Conditional Aggregation Operators * Lookup Aggregation Operators +* Convert Aggregation Operators At the time of this writing, we provide support for the following Aggregation Operations in Spring Data MongoDB: @@ -1732,6 +1733,8 @@ At the time of this writing, we provide support for the following Aggregation Op | Type Aggregation Operators | `type` +| Convert Aggregation Operators +| `convert`, `toBool`, `toDate`, `toDecimal`, `toDouble`, `toInt`, `toLong`, `toObjectId`, `toString` |=== * The operation is mapped or added by Spring Data MongoDB.