Browse Source

Retain type for bitwise update

issue/5146
Christoph Strobl 1 week ago
parent
commit
260782f54a
No known key found for this signature in database
GPG Key ID: E6054036D0C37A4B
  1. 39
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java
  2. 25
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUpdateTests.java

39
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java

@ -1047,6 +1047,19 @@ public class Update implements UpdateDefinition { @@ -1047,6 +1047,19 @@ public class Update implements UpdateDefinition {
return reference;
}
/**
* Updates to the result of a bitwise and operation between the current value and the given one.
*
* @param value
* @return never {@literal null}.
* @since 4.5.9
*/
public Update and(int value) {
addFieldOperation(BitwiseOperator.AND, value);
return reference;
}
/**
* Updates to the result of a bitwise or operation between the current value and the given one.
*
@ -1059,6 +1072,19 @@ public class Update implements UpdateDefinition { @@ -1059,6 +1072,19 @@ public class Update implements UpdateDefinition {
return reference;
}
/**
* Updates to the result of a bitwise or operation between the current value and the given one.
*
* @param value
* @return never {@literal null}.
* @since 4.5.9
*/
public Update or(int value) {
addFieldOperation(BitwiseOperator.OR, value);
return reference;
}
/**
* Updates to the result of a bitwise xor operation between the current value and the given one.
*
@ -1071,6 +1097,19 @@ public class Update implements UpdateDefinition { @@ -1071,6 +1097,19 @@ public class Update implements UpdateDefinition {
return reference;
}
/**
* Updates to the result of a bitwise xor operation between the current value and the given one.
*
* @param value
* @return never {@literal null}.
* @since 4.5.9
*/
public Update xor(int value) {
addFieldOperation(BitwiseOperator.XOR, value);
return reference;
}
private void addFieldOperation(BitwiseOperator operator, Number value) {
reference.addMultiFieldOperation(BIT_OPERATOR, key, new Document(operator.toString(), value));
}

25
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUpdateTests.java

@ -29,7 +29,6 @@ import org.junit.jupiter.api.Test; @@ -29,7 +29,6 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Version;
import org.springframework.data.domain.Sort;
@ -339,6 +338,30 @@ class MongoTemplateUpdateTests { @@ -339,6 +338,30 @@ class MongoTemplateUpdateTests {
.append("_class", "org.springframework.data.mongodb.core.MongoTemplateUpdateTests$Versioned"));
}
@Test // GH-5146
void bitOperatorShouldRetainInputType() {
MongoCollection<org.bson.Document> collection = collection(Score.class);
collection.insertOne(new org.bson.Document("_id", "bitwise-update-int").append("extraCredit", 10));
collection.insertOne(new org.bson.Document("_id", "bitwise-update-long").append("extraCredit", 10));
Query queryInt = Query.query(Criteria.where("_id").is("bitwise-update-int"));
Update updateInt = new Update().bitwise("extraCredit").or(100);
template.updateFirst(queryInt, updateInt, template.getCollectionName(Score.class));
Query queryLong = Query.query(Criteria.where("_id").is("bitwise-update-long"));
Update updateLong = new Update().bitwise("extraCredit").or(100L);
template.updateFirst(queryLong, updateLong, template.getCollectionName(Score.class));
org.bson.Document $project = org.bson.Document
.parse("{ $project : { 'extraCredit-field-type' : { $type : '$extraCredit'}} }");
ArrayList<org.bson.Document> fieldTypesAfterUpdate = collection.aggregate(List.of($project))
.into(new ArrayList<>());
assertThat(fieldTypesAfterUpdate).containsExactly(
new org.bson.Document("_id", "bitwise-update-int").append("extraCredit-field-type", "int"), //
new org.bson.Document("_id", "bitwise-update-long").append("extraCredit-field-type", "long"));
}
private List<org.bson.Document> all(Class<?> type) {
return collection(type).find(new org.bson.Document()).into(new ArrayList<>());
}

Loading…
Cancel
Save