Как упоминалось в комментарии D.SM , лучше создать новый MongoClient из устаревшего клиента, а не из строки подключения, поскольку не все параметры клиента имеют соответствующие параметры URI. Пока я пришел с:
private com.mongodb.client.MongoClient fromLegacyClient(MongoClient legacyMongoClient) {
MongoClientOptions mongoClientOptions = legacyMongoClient.getMongoClientOptions();
MongoClientSettings.Builder mongoClientSettingsBuilder = MongoClientSettings.builder()
.readPreference(legacyMongoClient.getReadPreference())
.writeConcern(legacyMongoClient.getWriteConcern())
.retryWrites(mongoClientOptions.getRetryWrites())
.readConcern(legacyMongoClient.getReadConcern())
.codecRegistry(mongoClientOptions.getCodecRegistry())
.compressorList(mongoClientOptions.getCompressorList())
.applicationName(mongoClientOptions.getApplicationName())
.commandListenerList(mongoClientOptions.getCommandListeners())
.codecRegistry(mongoClientOptions.getCodecRegistry())
.applyToClusterSettings(clusterSettings -> {
clusterSettings.hosts(legacyMongoClient.getAllAddress());
clusterSettings.localThreshold(mongoClientOptions.getLocalThreshold(), MILLISECONDS);
clusterSettings.serverSelectionTimeout(mongoClientOptions.getServerSelectionTimeout(), MILLISECONDS);
clusterSettings.serverSelector(mongoClientOptions.getServerSelector());
clusterSettings.requiredReplicaSetName(mongoClientOptions.getRequiredReplicaSetName());
mongoClientOptions.getClusterListeners().forEach(clusterSettings::addClusterListener);
})
.applyToConnectionPoolSettings(connectionPoolSettings -> {
mongoClientOptions.getConnectionPoolListeners().forEach(connectionPoolSettings::addConnectionPoolListener);
connectionPoolSettings.maxConnectionIdleTime(mongoClientOptions.getMaxConnectionIdleTime(), MILLISECONDS);
connectionPoolSettings.maxConnectionLifeTime(mongoClientOptions.getMaxConnectionLifeTime(), MILLISECONDS);
connectionPoolSettings.maxWaitTime(mongoClientOptions.getMaxWaitTime(), MILLISECONDS);
})
.applyToSocketSettings(socketSettings -> {
socketSettings.connectTimeout(mongoClientOptions.getConnectTimeout(), MILLISECONDS);
socketSettings.readTimeout(mongoClientOptions.getSocketTimeout(), MILLISECONDS);
})
.applyToServerSettings(serverSettings -> {
mongoClientOptions.getServerListeners().forEach(serverSettings::addServerListener);
serverSettings.minHeartbeatFrequency(mongoClientOptions.getMinHeartbeatFrequency(), MILLISECONDS);
serverSettings.heartbeatFrequency(mongoClientOptions.getHeartbeatFrequency(), MILLISECONDS);
})
.applyToSslSettings(sslSettings -> {
sslSettings.enabled(mongoClientOptions.isSslEnabled());
sslSettings.invalidHostNameAllowed(mongoClientOptions.isSslInvalidHostNameAllowed());
sslSettings.context(mongoClientOptions.getSslContext());
});
if (legacyMongoClient.getCredential() != null) {
mongoClientSettingsBuilder.credential(legacyMongoClient.getCredential());
}
return MongoClients.create(mongoClientSettingsBuilder.build());
}