Skip to content

Commit a47cff1

Browse files
committed
mongo client settings
1 parent b46f709 commit a47cff1

File tree

9 files changed

+165
-224
lines changed

9 files changed

+165
-224
lines changed

source/connection/mongoclient.txt

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,51 @@ MongoClient
3232
-----------
3333

3434
You can connect to and communicate with MongoDB using the ``MongoClient``
35-
class.
35+
class. To create a `MongoClientSettings
36+
<{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.html>`__ object, use the ``MongoClientSettings.builder()`` method and chain methods to specify your
37+
settings. After chaining them, use the ``build()`` method to create the
38+
``MongoClientSettings`` object.
3639

37-
Use the ``MongoClients.create()`` method to construct a ``MongoClient``.
40+
To learn about the different settings you can use to control the behavior of your ``MongoClient``, see the :ref:`Specify MongoClient Settings <specify-mongoclient-settings>` guide.
41+
42+
Example
43+
~~~~~~~
44+
45+
This example demonstrates specifying a ``ConnectionString``:
46+
47+
.. literalinclude:: /includes/fundamentals/code-snippets/MCSettings.java
48+
:start-after: begin ConnectionString
49+
:end-before: end ConnectionString
50+
:language: java
51+
:emphasize-lines: 3
52+
:dedent:
53+
54+
.. note:: Chain Order
55+
56+
Some options in the settings map to a connection string option.
57+
If you specify the same options in your settings and connection
58+
string, the order you chain them determines which option the driver
59+
uses. The driver uses the **last** setting it reads.
60+
61+
For example, this snippet contains settings with the following times
62+
for the driver to connect to an available socket:
63+
64+
- The connection string specifies within ``2 SECONDS``
65+
- The :ref:`socket settings <mcs-socket-settings>` specifies within
66+
``5 SECONDS``
67+
68+
.. code-block:: java
69+
:emphasize-lines: 2,4
70+
71+
MongoClient mongoClient = MongoClients.create(
72+
MongoClientSettings.builder().applyConnectionString(new ConnectionString("mongodb+srv://<db_username>:<db_password>@<hostname>:<port>/<auth db>?connectTimeoutMS=2000"))
73+
.applyToSocketSettings(builder ->
74+
builder.connectTimeout(5L, SECONDS))
75+
.build());
76+
77+
Since the driver reads the socket settings options last, the driver
78+
expects to connect to an available socket within ``5 SECONDS`` before
79+
timing out.
3880

3981
.. important:: Reuse Your Client
4082

@@ -44,10 +86,6 @@ Use the ``MongoClients.create()`` method to construct a ``MongoClient``.
4486
All resource usage limits, such as max connections, apply to individual
4587
``MongoClient`` instances.
4688

47-
To learn about the different settings you can use to control the
48-
behavior of your ``MongoClient``, see the
49-
:ref:`Specify MongoClient Settings <specify-mongoclient-settings>` guide.
50-
5189
.. tip::
5290

5391
Always call ``MongoClient.close()`` to clean up resources when an

source/connection/specify-connection-options.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ Specify Connection Options
66

77
.. toctree::
88

9-
MongoClient Settings
10-
</connection/specify-connection-options/mongoclientsettings>
119
Stable API </connection/specify-connection-options/stable-api>
1210
Connection Pools </connection/specify-connection-options/connection-pools>
1311
Cluster Settings </connection/specify-connection-options/cluster-settings>

source/connection/specify-connection-options/cluster-settings.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ String` or :guilabel:`MongoClientSettings` tab to see the options available:
8686
your :ref:`connection URI <connection-uri>` to use this option.
8787

8888
**Default**: ``mongodb``
89+
90+
This example connects the driver directly to a server,
91+
regardless of the type of MongoDB cluster it's a part of:
92+
93+
.. code-block:: java
94+
95+
ConnectionString connectionString = "mongodb://<host>:<port>/?directConnection=true"
96+
MongoClient mongoClient = MongoClients.create(connectionString)
97+
98+
For more information about these parameters, see the `ConnectionString
99+
<{+api+}/apidocs/mongodb-driver-core/com/mongodb/ConnectionString.html>`__
100+
API documentation.
89101

90102
.. tab:: MongoClientSettings
91103
:tabid: MongoClient
@@ -163,7 +175,7 @@ String` or :guilabel:`MongoClientSettings` tab to see the options available:
163175

164176
Throws an exception if you are not using the SRV connection protocol.
165177

166-
This example specifies for the driver to connect directly to a server,
178+
This example connects the driver directly to a server,
167179
regardless of the type of MongoDB cluster it's a part of:
168180

169181
.. literalinclude:: /includes/fundamentals/code-snippets/MCSettings.java

source/connection/specify-connection-options/mongoclientsettings.txt

Lines changed: 0 additions & 200 deletions
This file was deleted.

source/connection/specify-connection-options/network-compression.txt

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,14 @@ by specifying the algorithms in one of the following ways:
5656
- Use the ``compressors`` parameter in your connection string.
5757
- Chain the ``compressorList()`` method to the ``MongoClientSettings.builder()`` method.
5858

59-
This example shows how to specify the Snappy, Zstandard, and Zlib compression
60-
algorithms. Select the :guilabel:`Connection String` or :guilabel:`MongoClientSettings`
59+
Select the :guilabel:`Connection String` or :guilabel:`MongoClientSettings`
6160
tab to see the corresponding syntax:
6261

6362
.. tabs::
6463

6564
.. tab:: Connection String
6665
:tabid: uri
6766

68-
.. literalinclude:: /includes/connect/NetworkCompression.java
69-
:start-after: start-specify-connection-string
70-
:end-before: end-specify-connection-string
71-
:language: java
72-
7367
Include the following parameters in your connection string to enable compression:
7468

7569
.. list-table::
@@ -98,14 +92,40 @@ tab to see the corresponding syntax:
9892
values compressing slower (but resulting in smaller requests).
9993

10094
**Default**: ``null``
95+
96+
The following specifies the order in which the driver will attempt to
97+
compress requests before they are sent:
98+
99+
.. literalinclude:: /includes/connect/NetworkCompression.java
100+
:start-after: start-specify-connection-string
101+
:end-before: end-specify-connection-string
102+
:language: java
103+
104+
For more information about these parameters, see the `ConnectionString
105+
<{+api+}/apidocs/mongodb-driver-core/com/mongodb/ConnectionString.html>`__
106+
API documentation.
101107

102108
.. tab:: MongoClientSettings
103-
:tabid: mongoclientsettings
109+
:tabid: mongoclientsettings
110+
111+
Chain the following methods to your ``MongoClientSettings`` constructor to modify the driver's compression behavior:
112+
113+
.. list-table::
114+
:widths: 40 60
115+
:header-rows: 1
116+
117+
* - Method
118+
- Description
119+
120+
* - ``compressorList()``
121+
- Sets the compressors to use for compressing messages to the server.
122+
123+
.. literalinclude:: /includes/connect/NetworkCompression.java
124+
:start-after: start-specify-client-settings
125+
:end-before: end-specify-client-settings
126+
:language: java
104127

105-
.. literalinclude:: /includes/connect/NetworkCompression.java
106-
:start-after: start-specify-client-settings
107-
:end-before: end-specify-client-settings
108-
:language: java
128+
For more information, see the `MongoClientSettings.Builder <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.Builder.html>`__ API documentation.
109129

110130
.. _java-compression-dependencies:
111131

source/connection/specify-connection-options/server-settings.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,6 @@ Configuring Server Settings
9999
* - ``addServerMonitorListener()``
100100
- Adds a listener for server monitor-related events.
101101

102-
* - ``applyConnectionString()``
103-
- Uses the settings from a ``ConnectionString`` object.
104-
105102
* - ``applySettings()``
106103
- Uses the server settings specified in a ``ServerSettings`` object.
107104

source/includes/fundamentals/code-snippets/MCSettings.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ private static void createLoggerSettings() {
141141
////begin LoggerSettings
142142
MongoClient mongoClient = MongoClients.create(
143143
MongoClientSettings.builder().applyConnectionString(new ConnectionString("<your connection string>"))
144+
.applicationName("<application name>")
144145
.applyToLoggerSettings(builder ->
145146
builder.maxDocumentLength(5_000))
146147
.build());

0 commit comments

Comments
 (0)