Skip to content

Update with $unset fails with mapping exception #4565

Open
@csmager

Description

@csmager

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.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions