0

I am using the codec for the serialization and deserialization of POJO using 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);
    }
}

Trying to set applyToClusterSettings, but quite not sure how can I set the MongoDB URI

San Jaisy
  • 15,327
  • 34
  • 171
  • 290

1 Answers1

-1
.applyConnectionString(new ConnectionString(mongodbConfiguration.uri()))
San Jaisy
  • 15,327
  • 34
  • 171
  • 290
  • [A code-only answer is not high quality](//meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers). While this code may be useful, you can improve it by saying why it works, how it works, when it should be used, and what its limitations are. Please [edit] your answer to include explanation and link to relevant documentation. – Stephen Ostermiller Oct 22 '21 at 00:12