@@ -51,9 +51,11 @@ What Is the Difference Between "connectTimeoutMS", "socketTimeoutMS" and "maxTim
51
51
52
52
* - Setting
53
53
- Description
54
- * - connectTimeoutMS
55
- - ``connectTimeoutMS`` is a :ref:`connection option <node-connection-options>` that sets the time, in milliseconds,
56
- for an individual connection from your connection pool to establish a TCP connection to the MongoDB server before
54
+ * - **connectTimeoutMS**
55
+ - ``connectTimeoutMS`` is a :ref:`connection option
56
+ <node-connection-options>` that sets the time, in milliseconds,
57
+ for an individual connection from your connection pool to
58
+ establish a TCP connection to the MongoDB server before
57
59
timing out.
58
60
59
61
.. tip::
@@ -62,16 +64,18 @@ What Is the Difference Between "connectTimeoutMS", "socketTimeoutMS" and "maxTim
62
64
connection to a MongoDB server, use the ``serverSelectionTimeoutMS`` option instead.
63
65
64
66
**Default:** 10000
65
- * - socketTimeoutMS
67
+ * - ** socketTimeoutMS**
66
68
- ``socketTimeoutMS`` specifies the amount of time the driver waits
67
69
for an inactive socket before closing it. The default value is to
68
70
never time out the socket. This option applies only to sockets that
69
71
have already been connected.
70
- * - maxTimeMS
71
- - `maxTimeMS <{+api+}/classes/FindCursor.html#maxTimeMS>`__ is the maximum
72
- amount of time the server should wait for an operation to complete
73
- after it has reached the server. If an operation runs over the
74
- specified time limit, it returns a timeout error.
72
+ * - **maxTimeMS**
73
+ - `maxTimeMS <{+api+}/classes/FindCursor.html#maxTimeMS>`__
74
+ specifies the maximum amount of time the server
75
+ should wait for an operation to complete after it has reached the
76
+ server. If an operation runs over the specified time limit, it
77
+ returns a timeout error. You can pass ``maxTimeMS`` only to an
78
+ individual operation or to a cursor.
75
79
76
80
To specify the optional settings for your ``MongoClient``, declare one or
77
81
more available settings in the ``options`` object of the constructor as
@@ -88,6 +92,13 @@ To see all the available settings, see the
88
92
`MongoClientOptions <{+api+}/interfaces/MongoClientOptions.html>`__
89
93
API Documentation.
90
94
95
+ To specify ``maxTimeMS``, chain the ``maxTimeMS()`` method with a
96
+ timeout specification to an operation that returns a ``Cursor``:
97
+
98
+ .. code-block:: javascript
99
+
100
+ const cursor = collection.find({}).maxTimeMS(50);
101
+
91
102
How Can I Prevent the Driver From Hanging During Connection or From Spending Too Long Trying to Reach Unreachable Replica Sets?
92
103
-------------------------------------------------------------------------------------------------------------------------------
93
104
@@ -168,22 +179,8 @@ How Can I Prevent Long-Running Operations From Slowing Down the Server?
168
179
-----------------------------------------------------------------------
169
180
170
181
You can prevent long-running operations from slowing down the server by
171
- specifying a timeout value. You can use the ``maxTimeMS`` option setting in
172
- your ``MongoClient`` constructor to apply this to all the operations
173
- called by the client, or chain the ``maxTimeMS()`` method to an operation that
174
- returns a ``Cursor`` to apply to that specific one.
175
-
176
- To specify the optional ``maxTimeMS`` setting for your ``MongoClient``,
177
- declare it in the ``options`` object of the constructor as follows:
178
-
179
- .. code-block:: javascript
180
-
181
- const client = new MongoClient(uri, {
182
- connectTimeoutMS: <integer value>,
183
- socketTimeoutMS: <integer value>,
184
- maxTimeMS: <integer value>,
185
- });
186
-
182
+ specifying a timeout value. You can chain the ``maxTimeMS()`` method to
183
+ an operation that returns a ``Cursor`` to set a timeout on a specific action.
187
184
188
185
The following example shows how you can chain the ``maxTimeMS()`` method
189
186
to an operation that returns a ``Cursor``:
@@ -309,7 +306,6 @@ to reach ``server1``, ``server2``, and ``server3``.
309
306
]
310
307
}
311
308
312
-
313
309
If you are unable to find the answer to your question here, try our forums and
314
310
support channels listed in the :doc:`Issues and Help <issues-and-help>`
315
311
section.
0 commit comments