From 60291b692e0493e5716e55cf51d0c256597ce66d Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Tue, 27 Jan 2026 10:55:03 +0100 Subject: [PATCH 1/2] Prepare issue branch --- pom.xml | 2 +- spring-data-mongodb-distribution/pom.xml | 2 +- spring-data-mongodb/pom.xml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) 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 From 260782f54a8c6ca191c0a7c3d8dcc2cf02ae4e0a Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Tue, 27 Jan 2026 11:47:02 +0100 Subject: [PATCH 2/2] Retain type for bitwise update --- .../data/mongodb/core/query/Update.java | 39 +++++++++++++++++++ .../core/MongoTemplateUpdateTests.java | 25 +++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) 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 33ea42780..d589f4e45 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 @@ -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 { 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 { 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<>()); }