@ -18,8 +18,10 @@ package org.springframework.data.mongodb.core.aggregation;
@@ -18,8 +18,10 @@ package org.springframework.data.mongodb.core.aggregation;
import java.util.Arrays ;
import java.util.Collection ;
import java.util.Collections ;
import java.util.Set ;
import org.bson.Document ;
import org.springframework.data.mongodb.core.aggregation.Aggregation.SystemVariable ;
import org.springframework.util.Assert ;
/ * *
@ -135,6 +137,26 @@ public class ObjectOperators {
@@ -135,6 +137,26 @@ public class ObjectOperators {
public GetField getField ( String fieldName ) {
return GetField . getField ( fieldName ) . from ( value ) ;
}
/ * *
* Creates new { @link SetField aggregation expression } that takes the associated value and obtains the value of the
* field with matching name .
*
* @since 4 . 0
* /
public SetField setField ( String fieldName ) {
return SetField . setField ( fieldName ) . of ( value ) ;
}
/ * *
* Creates new { @link SetField aggregation expression } that takes the associated value and obtains the value of the
* field with matching name .
*
* @since 4 . 0
* /
public AggregationExpression removeField ( String fieldName ) {
return SetField . setField ( fieldName ) . of ( value ) . toValue ( SystemVariable . REMOVE ) ;
}
}
/ * *
@ -332,4 +354,54 @@ public class ObjectOperators {
@@ -332,4 +354,54 @@ public class ObjectOperators {
return "$getField" ;
}
}
/ * *
* { @link AggregationExpression } for { @code $setField } .
*
* @author Christoph Strobl
* @since 4 . 0
* /
public static class SetField extends AbstractAggregationExpression {
protected SetField ( Object value ) {
super ( value ) ;
}
public static SetField setField ( String fieldName ) {
return new SetField ( Collections . singletonMap ( "field" , fieldName ) ) ;
}
public static SetField setField ( Field field ) {
return setField ( field . getTarget ( ) ) ;
}
public SetField of ( String fieldRef ) {
return of ( Fields . field ( fieldRef ) ) ;
}
public SetField of ( AggregationExpression expression ) {
return of ( ( Object ) expression ) ;
}
private SetField of ( Object fieldRef ) {
return new SetField ( append ( "input" , fieldRef ) ) ;
}
public SetField toValueOf ( String fieldReference ) {
return toValue ( Fields . field ( fieldReference ) ) ;
}
public SetField toValueOf ( AggregationExpression expression ) {
return toValue ( expression ) ;
}
public SetField toValue ( Object value ) {
return new SetField ( append ( "value" , value ) ) ;
}
@Override
protected String getMongoMethod ( ) {
return "$setField" ;
}
}
}