【问题标题】:How to Set the URI for the ClusterSettings with codec in mongodb java driver如何在 mongodb java 驱动程序中使用编解码器设置 ClusterSettings 的 URI
【发布时间】:2021-10-21 16:22:12
【问题描述】:

我正在使用编解码器进行 POJO 的序列化和反序列化,使用 4.3/driver-reactive http://mongodb.github.io/mongo-java-driver/4.3/driver-reactive/getting-started/quick-start-pojo/

@Singleton
public record Repository(MongodbConfiguration mongodbConfiguration) implements IRepository {
    @Override
    public <T> MongoCollection<T> getCollection(String collectionName, Class<T> typeParameterClass) {
        // create codec registry for POJOs
        CodecRegistry pojoCodecRegistry = fromProviders(PojoCodecProvider.builder().automatic(true).build());
        CodecRegistry codecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(), pojoCodecRegistry);

        MongoClientSettings clientSettings = MongoClientSettings.builder()
                .applyToClusterSettings() // need to set the URI
                .codecRegistry(codecRegistry)
                .addCommandListener(new MongoCommand())
                .build();
        MongoClient mongoClient = MongoClients.create(clientSettings);

        return mongoClient
                .getDatabase(mongodbConfiguration.database())
                .getCollection(collectionName, typeParameterClass);
    }
}

尝试设置applyToClusterSettings,但不太确定如何设置 MongoDB URI

【问题讨论】:

    标签: java micronaut mongodb-java mongodb-java-3.3.0 mongodb-java-3.8


    【解决方案1】:
    .applyConnectionString(new ConnectionString(mongodbConfiguration.uri()))
    

    【讨论】:

    • A code-only answer is not high quality。虽然此代码可能很有用,但您可以通过说明其工作原理、工作方式、何时应该使用以及它的局限性来改进它。请edit您的回答包括解释和相关文档的链接。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-28
    • 1970-01-01
    • 2014-02-26
    • 2016-03-13
    • 2017-08-13
    • 2018-07-18
    相关资源
    最近更新 更多