how to solve mongodb key contain dot

mongodb 插入数据,数据中含有dot,mongodb 报错

springboot解决方式:不走springboot默认配置,重新初始化一份mongodb的配置

@Configuration
@EnableMongoRepositories(basePackages = "xx.xx.xx")
public class MongoDbConfig extends AbstractMongoConfiguration {

    @Value("${spring.data.mongodb.url}")
    private String url;

    @Value("${spring.data.mongodb.database}")
    private String databaseName;

    @Override
    protected String getDatabaseName() {
        return databaseName;
    }

    @Override
    public MongoClient mongoClient() {
        return new MongoClient(new MongoClientURI(url));
    }

    @Override
    protected Collection getMappingBasePackages() {
        return Arrays.asList("xx.xx.xx.model");
    }


    /**
     * 解决mongodb map中不能存储 . 问题
     *
     * @return
     * @throws Exception
     */
    @Override
    public MappingMongoConverter mappingMongoConverter() throws Exception {
        DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory());
        MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mongoMappingContext());
        converter.setCustomConversions(customConversions());
        converter.setMapKeyDotReplacement("@dot@");
        return converter;
    }
}

你可能感兴趣的:(how to solve mongodb key contain dot)