diff --git a/pom.xml b/pom.xml
index 6a61f22b0..b02255105 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
org.springframework.data
spring-data-mongodb-parent
- 5.1.0-SNAPSHOT
+ 5.1.x-GH-5146-SNAPSHOT
pom
Spring Data MongoDB
diff --git a/spring-data-mongodb-distribution/pom.xml b/spring-data-mongodb-distribution/pom.xml
index cab02fe27..bfc01feb0 100644
--- a/spring-data-mongodb-distribution/pom.xml
+++ b/spring-data-mongodb-distribution/pom.xml
@@ -15,7 +15,7 @@
org.springframework.data
spring-data-mongodb-parent
- 5.1.0-SNAPSHOT
+ 5.1.x-GH-5146-SNAPSHOT
../pom.xml
diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml
index 0a758111a..e8aa7cccd 100644
--- a/spring-data-mongodb/pom.xml
+++ b/spring-data-mongodb/pom.xml
@@ -13,7 +13,7 @@
org.springframework.data
spring-data-mongodb-parent
- 5.1.0-SNAPSHOT
+ 5.1.x-GH-5146-SNAPSHOT
../pom.xml
diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java
index df2bf90f7..7d9ca1571 100644
--- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java
+++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Update.java
@@ -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 {
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 {
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));
}
diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUpdateTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUpdateTests.java
index 6a0ba7bb9..0f94fbada 100644
--- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUpdateTests.java
+++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateUpdateTests.java
@@ -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 {
.append("_class", "org.springframework.data.mongodb.core.MongoTemplateUpdateTests$Versioned"));
}
+ @Test // GH-5146
+ void bitOperatorShouldRetainInputType() {
+
+ MongoCollection 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 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 all(Class> type) {
return collection(type).find(new org.bson.Document()).into(new ArrayList<>());
}