Browse Source

Merge 260782f54a into 2cb6184d45

pull/5153/merge
Christoph Strobl 3 days ago committed by GitHub
parent
commit
2552925c66
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 2
      pom.xml
  2. 2
      spring-data-mongodb-distribution/pom.xml
  3. 2
      spring-data-mongodb/pom.xml
  4. 39
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java
  5. 25
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUpdateTests.java

2
pom.xml

@ -5,7 +5,7 @@ @@ -5,7 +5,7 @@
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>5.1.0-SNAPSHOT</version>
<version>5.1.x-GH-5146-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Spring Data MongoDB</name>

2
spring-data-mongodb-distribution/pom.xml

@ -15,7 +15,7 @@ @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>5.1.0-SNAPSHOT</version>
<version>5.1.x-GH-5146-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

2
spring-data-mongodb/pom.xml

@ -13,7 +13,7 @@ @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>5.1.0-SNAPSHOT</version>
<version>5.1.x-GH-5146-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

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

@ -1331,6 +1331,19 @@ public class Update implements UpdateDefinition { @@ -1331,6 +1331,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.
*
@ -1343,6 +1356,19 @@ public class Update implements UpdateDefinition { @@ -1343,6 +1356,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.
*
@ -1355,6 +1381,19 @@ public class Update implements UpdateDefinition { @@ -1355,6 +1381,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