@@ -52,20 +52,36 @@ the settings object to the ``MongoClient.create()`` method.
52
52
Configuring the connection this way makes it easier to
53
53
change settings at runtime and can help you catch errors at compile time.
54
54
55
- The following example shows how to set options when creating a
56
- ``MongoClientSettings`` instance:
55
+ The following example shows how to specify your connection target and
56
+ set other options when creating a ``MongoClientSettings`` instance:
57
57
58
58
.. code-block:: kotlin
59
59
60
- val uri = "<connection string>"
61
-
62
60
val settings = MongoClientSettings.builder()
63
- .applyConnectionString(ConnectionString(uri))
64
- .retryWrites(true)
61
+ .applyToClusterSettings { builder -> builder.hosts(listOf(ServerAddress("localhost", 27017))) }
62
+ .applyToSocketSettings { builder -> builder.connectTimeout(60000, TimeUnit.MILLISECONDS) }
63
+ .applyToSslSettings { builder -> builder.enabled(true) }
65
64
.build()
66
65
67
66
val mongoClient = MongoClient.create(settings)
68
67
68
+ If you prefer to provide a connection string instead of specifying
69
+ the hostname and port, you can use the ``applyConnectionString()``
70
+ method, then set other options by using builder methods, as shown in the
71
+ following code:
72
+
73
+ .. code-block:: kotlin
74
+ :emphasize-lines: 1, 3
75
+
76
+ val uri = "<connection string>"
77
+ val settings = MongoClientSettings.builder()
78
+ .applyConnectionString(ConnectionString(uri))
79
+ .applyToSocketSettings { builder -> builder.connectTimeout(60000, TimeUnit.MILLISECONDS) }
80
+ .applyToSslSettings { builder -> builder.enabled(true) }
81
+ .build()
82
+
83
+ val mongoClient = MongoClient.create(settings)
84
+
69
85
Connection Options
70
86
------------------
71
87
0 commit comments