0

I have json data where the key contains dots as '123.456' and value as object.

  {
        "models": {
            "123.456": [{
                "key1": "value1",
                "key2": "value2"
            }]
        }
    }

I am getting an mapping exception when trying to save the object in MongoDB as following

org.springframework.data.mapping.model.MappingException: Map key 123.456 contains dots but no replacement was configured!Make sure map keys don't contain dots in the first place or configure an appropriate replacement!

Is there any support to save the key as a field containing dots in Spring Mongo?

shashank
  • 379
  • 5
  • 6
  • 15

1 Answers1

0

You have to use MappingMongoConverter class. It has the setMapKeyDotReplacement(String mapKeyDotReplacement) method.

According to the Spring-MongoDB Docs:

The setMapKeyDotReplacement(String mapKeyDotReplacement)method checks whether the dots are present in a Map or not. If found, then it should be replaced with the character specified.

Example of how to use it:

@Component
public class Example {

    @Autowired
    private MappingMongoConverter mappingMongoConverter;

    public void replaceKeyContainingDot() {
      mappingMongoConverter.setMapKeyDotReplacement("pass here the character you want to replace the dots with");
    }

} 

Note : From MongoDB Docs :

The use of $ and . in field names is not recommended and is not supported by the official MongoDB drivers.

There is a workaround that I found from documentation. You may have to override potentiallyEscapeMapKey(String source) and potentiallyUnescapeMapKey(String source) methods from MappingMongoCoverter class.

Please refer to this popular SO questions here for more information and better understanding :

  1. How to customize MappingMongoConverter (setMapKeyDotReplacement) in Spring-Boot without breaking the auto-configuration?

  2. MongoDB-Escape dots '.' in map key

Anish B.
  • 9,111
  • 3
  • 21
  • 41
  • 1
    But I do not want to replace dots instead I would like to save dots as it is and when I used the mongoMappingConverter.setMapKeyDotReplacement(".") I am getting Invalid bson field dot exception. – shashank Oct 10 '19 at 02:25
  • From mongo db documentation, the use of $ and . in field names is not recommended and is not supported by the official MongoDB drivers. – Anish B. Oct 10 '19 at 02:43
  • Mongo 3.6 allows using dots and dollar signs in field names https://docs.mongodb.com/manual/core/document/ – shashank Oct 10 '19 at 02:47
  • @shashank So, are you using MongoDB 3.6 ? – Anish B. Oct 10 '19 at 02:50
  • @shashank See this : https://stackoverflow.com/questions/12397118/mongodb-dot-in-key-name – Anish B. Oct 10 '19 at 02:53
  • @shashank Workaround is there but I'm not sure. Find a way to override the method `potentiallyEscapeMapKey(String source )`. – Anish B. Oct 10 '19 at 03:03
  • 1
    yes I was going through the documentation and thought of trying with this method potentiallyEscapeMapKey(".") – shashank Oct 10 '19 at 03:06