spring-projects / spring-projects/spring-data-mongodb
Update with $unset fails with mapping exception
Open
@christophstrobl is already working on this.
Since Nov 20, 2023.
status: pending-design-work
type: bug
- Dominant language
- Java
- Stars
- 1.7k
- Forks
- 1.1k
- PR merge metrics
- No merged PRs in 30d
Description
I'm using spring-data-mongodb 4.0.8. On creating an update with an $unset, the following is added to the document:
{ $unset: { fieldName: 1 } }
The value here - 1 - is then attempted to be mapped to the correct type for that field. In my case, that's Decimal128. I get this stack trace:
org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Integer] to type [org.bson.types.Decimal128] for value [1]
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:47)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:192)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:175)
at org.springframework.data.mongodb.core.convert.QueryMapper.applyFieldTargetTypeHintToValue(QueryMapper.java:848)
at org.springframework.data.mongodb.core.convert.QueryMapper.getMappedValue(QueryMapper.java:436)
at org.springframework.data.mongodb.core.convert.QueryMapper.getMappedObjectForField(QueryMapper.java:339)
at org.springframework.data.mongodb.core.convert.UpdateMapper.getMappedObjectForField(UpdateMapper.java:160)
at org.springframework.data.mongodb.core.convert.QueryMapper.getMappedObject(QueryMapper.java:166)
at org.springframework.data.mongodb.core.convert.UpdateMapper.getMappedObject(UpdateMapper.java:66)
at org.springframework.data.mongodb.core.convert.QueryMapper.convertSimpleOrDocument(QueryMapper.java:578)
at org.springframework.data.mongodb.core.convert.QueryMapper.getMappedKeyword(QueryMapper.java:399)
at org.springframework.data.mongodb.core.convert.QueryMapper.getMappedObject(QueryMapper.java:146)
at org.springframework.data.mongodb.core.convert.UpdateMapper.getMappedObject(UpdateMapper.java:66)
at org.springframework.data.mongodb.core.QueryOperations$UpdateContext.getMappedUpdate(QueryOperations.java:864)
at org.springframework.data.mongodb.core.MongoTemplate.doUpdate(MongoTemplate.java:1617)
at org.springframework.data.mongodb.core.MongoTemplate.updateFirst(MongoTemplate.java:1540)
The value here does not relate to the field value, it should not be mapped. My current workaround is to override unset as below:
var update = new Update() {
@Override
public Update unset(String key) {
addMultiFieldOperation("$unset", key, "");
return this;
}
};
The mapping does not fail with an empty string, and an empty string is what's used in the examples in the docs.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.