@@ -36,13 +36,25 @@ What Is the Difference Between "connectTimeoutMS", "socketTimeoutMS" and "maxTim
36
36
after it has reached the server. If an operation runs over the
37
37
specified time limit, it returns a timeout error.
38
38
39
+ To specify the optional settings for your ``MongoClient``, declare one or
40
+ more of them in the ``options`` object of the constructor as follows:
41
+
42
+ .. code-block:: javascript
43
+
44
+ const client = new MongoClient(uri, {
45
+ connectTimeoutMS: <integer value>,
46
+ socketTimeoutMS: <integer value>,
47
+ maxTimeMS: <integer value>,
48
+ });
49
+
50
+
39
51
How Can I Prevent the Driver From Hanging During Connection or From Spending Too Long Trying to Reach Unreachable Replica Sets?
40
52
-------------------------------------------------------------------------------------------------------------------------------
41
53
42
54
To prevent the driver from hanging during connection or to prevent the
43
55
driver from spending too long trying to reach unreachable replica sets,
44
56
you can set the ``connectTimeoutMS`` option of your
45
- :doc:`connection options </fundamentals/connection#connection-options>`.
57
+ :doc:`connection options </fundamentals/connection#connection-options>`.
46
58
Generally, you should ensure that the
47
59
``connectTimeoutMS`` setting is not lower than the longest network
48
60
latency you have to a member of the set. If one of the secondary members
@@ -80,6 +92,7 @@ through the driver.
80
92
81
93
How Can I Prevent Sockets From Timing out Before They Become Active?
82
94
--------------------------------------------------------------------
95
+
83
96
Having a large connection pool does not always reduce reconnection
84
97
requests. Consider the following example:
85
98
@@ -94,25 +107,49 @@ One message every 3000 milliseconds is not enough to keep the sockets
94
107
active, so several of the sockets will time out after 5000 milliseconds.
95
108
Reduce the ``poolSize`` in the connection settings to fix the problem.
96
109
110
+ To specify the optional ``poolSize`` setting for your ``MongoClient``, declare
111
+ it in the ``options`` object of the constructor as follows:
112
+
113
+ .. code-block:: javascript
114
+
115
+ const client = new MongoClient(uri, {
116
+ poolSize: <integer value>,
117
+ });
118
+
119
+
97
120
What Does a Value of "0" mean for "connectTimeoutMS" and "socketTimeoutMS"?
98
121
---------------------------------------------------------------------------
99
122
100
- Setting ``connectTimeoutMS`` and ``socketTimeoutMS`` to the value ``0`` has
101
- causes the application to use the operating system's default socket timeout value.
123
+ If you set the value of ``connectTimeoutMS`` or ``socketTimeoutMS`` to
124
+ ``0``, your application will use the operating system's default socket
125
+ timeout value.
102
126
103
127
How Can I Prevent Long-Running Operations From Slowing Down the Server?
104
128
-----------------------------------------------------------------------
105
129
106
- By using ``maxTimeMS()`` you can prevent long-running operations from
107
- slowing down the server. This method allows the MongoDB server to cancel
108
- operations that run for more than the set value of ``maxTimeMS``.
130
+ You can prevent long-running operations from slowing down the server by
131
+ specifying a timeout value. You can use the ``maxTimeMS`` option setting in
132
+ your ``MongoClient`` constructor to apply this to all the operations
133
+ called by the client, or chain the ``maxTimeMS()`` method to an operation that
134
+ returns a ``Cursor`` to apply to that specific one.
135
+
136
+ To specify the optional ``maxTimeMS`` setting for your ``MongoClient``,
137
+ declare it in the ``options`` object of the constructor as follows:
138
+
139
+ .. code-block:: javascript
109
140
110
- The following example demonstrates how to use MaxTimeMS with a find
111
- operation:
141
+ const client = new MongoClient(uri, {
142
+ connectTimeoutMS: <integer value>,
143
+ socketTimeoutMS: <integer value>,
144
+ maxTimeMS: <integer value>,
145
+ });
146
+
147
+
148
+ The following example shows how you can chain the ``maxTimeMS()`` method
149
+ to an operation that returns a ``Cursor``:
112
150
113
151
.. literalinclude:: /code-snippets/faq/maxTimeMS-example.js
114
152
:language: javascript
115
- :linenos:
116
153
117
154
What Does the "keepAlive" Setting Do?
118
155
---------------------------------------
@@ -185,6 +222,21 @@ other, faster operations.
185
222
If the number of operations is greater than the set ``poolSize`` and
186
223
a slow operation occurs, subsequent operations will be delayed.
187
224
225
+
226
+ To create a separate connection pool, instantiate another ``MongoClient``
227
+ call the ``connect()`` method on it. See the following example for the
228
+ syntax you can use to create two clients, each with its own connection
229
+ pool:
230
+
231
+ .. code-block:: javascript
232
+
233
+ const clientA = new MongoClient(uri, options);
234
+ clientA.connect(); // any method calls on clientA use clientA's connection pool
235
+
236
+ const clientB = new MongoClient(uri, options);
237
+ clientB.connect(); // any method calls on clientB use clientB's connection pool
238
+
239
+
188
240
How Can I Ensure My Connection String Is Valid for a Replica Set?
189
241
-----------------------------------------------------------------
190
242
@@ -216,3 +268,8 @@ to reach ``server1``, ``server2``, and ``server3``.
216
268
}
217
269
]
218
270
}
271
+
272
+
273
+ If you are unable to find the answer to your question here, try our forums and
274
+ support channels listed in the :doc:`Issues and Help <issues-and-help>`
275
+ section.
0 commit comments