SpringBoot 集成 MongoDB 报 MongoDB-Escape dots ‘.’ in map key]

  • Post author:
  • Post category:其他


org.springframework.data.mapping.model.MappingException: Map key foo.bar.key contains dots but no replacement was configured! Make sure map keys don't contain dots in the first place or configure an appropriate replacement! at 
org.springframework.data.mongodb.core.convert.MappingMongoConverter.potentiallyEscapeMapKey(MappingMongoConverter.java:622) at 
org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeMapInternal(MappingMongoConverter.java:586) at 
org.springframework.data.mongodb.core.convert.MappingMongoConverter.createMap(MappingMongoConverter.java:517) at 
org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:424) at 
org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:386) at 
org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:373) at 
org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:257) at org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:373) at 
org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:451) at 
org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:386) at 
org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:373) at 
org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:257) at 
org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:373) at 
org.springframework.data.mongodb.core.convert.MappingMongoConverter.writePropertyInternal(MappingMongoConverter.java:451) at 
org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:386) at 
org.springframework.data.mongodb.core.convert.MappingMongoConverter$3.doWithPersistentProperty(MappingMongoConverter.java:373) at 
org.springframework.data.mapping.model.BasicPersistentEntity.doWithProperties(BasicPersistentEntity.java:257) at 
org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:373) at 
org.springframework.data.mongodb.core.convert.MappingMongoConverter.writeInternal(MappingMongoConverter.java:345) at 
org.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:310) at 
org.springframework.data.mongodb.core.convert.MappingMongoConverter.write(MappingMongoConverter.java:77) at 
org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:859) at 
org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:806) at 
org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:794)

当我们尝试插入值时,会发生这种情况。我们怎么解决这个问题?

在MongoDB中,dot始终被视为一个特殊字符,因此避免使用它很可能会在将来为您节省一些其他麻烦。

编辑:要覆盖默认值,请MappingMongoConverter添加以下bean声明:

在这里插入图片描述


代码:

@Configuration
public class MongoConfig {


    @Autowired
    private MongoDbFactory mongoFactory;

    @Autowired
    private MongoMappingContext mongoMappingContext;

    @Bean
    public MappingMongoConverter mongoConverter() throws Exception {
        DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoFactory);
        MappingMongoConverter mongoConverter = new MappingMongoConverter(dbRefResolver, mongoMappingContext);
        //this is my customization
        mongoConverter.setMapKeyDotReplacement("_");
        mongoConverter.afterPropertiesSet();
        return mongoConverter;
    }

}



版权声明:本文为qq_43340245原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。