how to solve mongodb key contain

2018-11-29  本文已影响0人  woshi开心

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<String> 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;
    }
}
上一篇下一篇

猜你喜欢

热点阅读