Skip to content

Commit 2ba171c

Browse files
committed
merge conflict plus uuid
2 parents da4b6f2 + dc5e924 commit 2ba171c

File tree

19 files changed

+1393
-1325
lines changed

19 files changed

+1393
-1325
lines changed

config/redirects

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ raw: ${prefix}/stable -> ${base}/current/
4444
[*-master]: ${prefix}/${version}/fundamentals/crud/read-operations/ -> ${base}/${version}/crud/query/
4545
[*-master]: ${prefix}/${version}/fundamentals/crud/query-document/ -> ${base}/${version}/crud/query/query-document/
4646
[*-master]: ${prefix}/${version}/fundamentals/crud/compound-operations/ -> ${base}/${version}/crud/compound-operations/
47-
[*-master]: ${prefix}/${version}/fundamentals/crud/read-write-pref/ -> ${base}/${version}/crud/read-write-pref/
47+
[*-master]: ${prefix}/${version}/fundamentals/crud/read-write-pref/ -> ${base}/${version}/crud/configure/
4848
[*-master]: ${prefix}/${version}/fundamentals/transactions/ -> ${base}/${version}/crud/transactions/
4949
[*-master]: ${prefix}/${version}/fundamentals/gridfs/ -> ${base}/${version}/crud/gridfs/
5050
[*-master]: ${prefix}/${version}/fundamentals/crud/read-operations/retrieve/ -> ${base}/${version}/crud/query/retrieve/

source/code-snippets/monitoring/apm-subscribe.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ const client = new MongoClient(uri, { monitorCommands:true });
1010
// Replace <event name> with the name of the event you are subscribing to
1111
const eventName = "<event name>";
1212

13-
// Subscribe to a specified event and print a message when the event is received
13+
// Subscribes to a specified event and print a message when the event is received
1414
client.on(eventName, event => console.log(event));
1515

1616
async function run() {
1717
try {
18-
// Establish and verify connection to the "admin" database
18+
// Establishes and verifies connection to the "admin" database
1919
await client.db("admin").command({ ping: 1 });
2020
console.log("Connected successfully");
2121
} finally {
22-
// Close the database connection on completion or error
22+
// Closes the database connection on completion or error
2323
await client.close();
2424
}
2525
}

source/code-snippets/monitoring/cpm-subscribe.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ const client = new MongoClient(uri);
99
// Replace <event name> with the name of the event you are subscribing to
1010
const eventName = "<event name>";
1111

12-
// Subscribe to the event
12+
// Subscribes to the event
1313
client.on(eventName, (event) =>
1414
console.log("\nreceived event:\n", event)
1515
);
1616

1717
async function run() {
1818
try {
19-
// Establish and verify connection
19+
// Establishes and verifies connection
2020
await client.db("admin").command({ ping: 1 });
2121
console.log("\nConnected successfully!\n");
2222
} finally {

source/code-snippets/monitoring/sdam-subscribe.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ const client = new MongoClient(uri);
1010
// Replace <event name> with the name of the event you are subscribing to
1111
const eventName = "<event name>";
1212

13-
// Subscribe to a specified event and print a message when the event is received
13+
// Subscribes to a specified event and prints a message when the event is received
1414
client.on(eventName, event => {
1515
console.log(`received ${eventName}: ${JSON.stringify(event, null, 2)}`);
1616
});
1717

1818
async function run() {
1919
try {
20-
// Establish and verify connection to the database
20+
// Establishes and verifies connection to the database
2121
await client.db("admin").command({ ping: 1 });
2222
console.log("Connected successfully");
2323
} finally {
24-
// Close the database connection on completion or error
24+
// Closes the database connection on completion or error
2525
await client.close();
2626
}
2727
}
Lines changed: 207 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
.. _node-connection-pools:
22

3-
================
4-
Connection Pools
5-
================
3+
========================================
4+
Manage Connections with Connection Pools
5+
========================================
66

77
.. facet::
88
:name: genre
@@ -31,62 +31,167 @@ are created by {+driver-short+}.
3131

3232
.. _node-faq-connection-pool:
3333

34-
Connection Pool Overview
35-
-------------------------
36-
37-
Every ``MongoClient`` instance has a built-in connection pool for each server
38-
in your MongoDB topology. Connection pools open sockets on demand to
39-
support concurrent requests to MongoDB in your application.
40-
41-
The maximum size of each connection pool is set by the ``maxPoolSize`` option, which
42-
defaults to ``100``. If the number of in-use connections to a server reaches
43-
the value of ``maxPoolSize``, the next request to that server will wait
44-
until a connection becomes available.
45-
46-
In addition to the sockets needed to support your application's requests,
47-
each ``MongoClient`` instance opens two more sockets per server
48-
in your MongoDB topology for monitoring the server's state.
49-
For example, a client connected to a three-node replica set opens six
50-
monitoring sockets. If the application uses the default setting for
51-
``maxPoolSize`` and only queries the primary (default) node, then
52-
there can be at most ``106`` total connections in the connection pool. If the
53-
application uses a :ref:`read preference <read-preference>` to query the
54-
secondary nodes, those connection pools grow and there can be
55-
``306`` total connections.
34+
Configure Connection Pools
35+
--------------------------
36+
37+
Every ``MongoClient`` instance has a built-in connection pool for each server in
38+
your MongoDB topology. If you do not configure the ``minPoolSize`` option,
39+
connection pools open sockets on demand to support concurrent requests to
40+
MongoDB in your application.
41+
42+
You can specify the following connection pool settings in your ``MongoClient``
43+
instance:
44+
45+
.. list-table::
46+
:widths: 30 70
47+
:header-rows: 1
48+
49+
* - Setting
50+
- Description
51+
52+
* - ``maxPoolSize``
53+
- | The maximum number of concurrent connections that the pool maintains.
54+
If the number of in-use connections to a server reaches the specified
55+
value, the next request to that server waits until a connection becomes
56+
available.
57+
|
58+
| **Default**: ``100``
59+
60+
* - ``maxConnecting``
61+
- | The maximum number of connections that each pool can establish
62+
concurrently.
63+
64+
* - ``minPoolSize``
65+
- | The minimum number of concurrent connections that the pool maintains.
66+
|
67+
| **Default**: ``0``
68+
69+
* - ``maxIdleTimeMS``
70+
- | The maximum number of milliseconds that a connection can remain idle in
71+
the pool.
72+
|
73+
| **Default**: ``0`` (no limit)
74+
75+
* - ``waitQueueTimeoutMS``
76+
- | The maximum number of milliseconds that a request can wait for a socket
77+
to become available.
78+
|
79+
| **Default**: ``0`` (no limit)
80+
81+
82+
.. _node-connection-pool-max-pool-size:
83+
84+
maxPoolSize
85+
~~~~~~~~~~~
86+
87+
In addition to the sockets needed to support your application's requests, each
88+
``MongoClient`` instance opens up to two connections per server in your
89+
MongoDB topology for monitoring the server's state.
90+
91+
For example, a client connected to a three-node replica set opens six monitoring
92+
sockets. If the application uses the default setting for ``maxPoolSize`` and
93+
only queries the primary (default) node, then there can be at most ``106`` open
94+
sockets and ``100`` connections in the connection pool. If the application uses
95+
a :ref:`read preference <read-preference>` to query the secondary nodes, those
96+
connection pools grow and there can be ``306`` total connections including the
97+
open monitoring sockets.
5698

5799
To support high numbers of concurrent MongoDB requests
58100
within one process, you can increase ``maxPoolSize``.
59101

60-
Connection pools are rate-limited. The ``maxConnecting`` option
61-
determines the number of connections that the pool can create in
62-
parallel at any time. For example, if the value of ``maxConnecting`` is
63-
``2``, the third request that attempts to concurrently check out a
64-
connection succeeds only when one the following cases occurs:
102+
The following code creates a ``MongoClient`` instance with a maximum connection
103+
pool size of ``200`` by specifying the ``maxPoolSize`` option in the
104+
``options`` object:
105+
106+
.. code-block:: javascript
107+
108+
const { MongoClient } = require('mongodb');
109+
110+
const uri = '<connection-string>';
111+
const client = new MongoClient(uri, {
112+
maxPoolSize: 200
113+
});
114+
115+
.. _node-connection-pool-max-connecting:
116+
117+
maxConnecting
118+
~~~~~~~~~~~~~
119+
120+
Connection pools rate-limit connection establishment. The ``maxConnecting``
121+
option determines the number of connections that the pool can create in parallel
122+
at any time. For example, if the value of ``maxConnecting`` is ``2``, the third
123+
request that attempts to concurrently check out a connection succeeds only when
124+
one the following cases occurs:
65125

66126
- The connection pool finishes creating a connection and there are fewer
67127
than ``maxPoolSize`` connections in the pool.
68128
- An existing connection is checked back into the pool.
69-
- The driver's ability to reuse existing connections improves due to
70-
rate-limits on connection creation.
71129

72-
You can set the minimum number of concurrent connections to
73-
each server with the ``minPoolSize`` option, which defaults to ``0``.
74-
The driver initializes the connection pool with this number of sockets. If
75-
sockets are closed, causing the total number
76-
of sockets (both in use and idle) to drop below the minimum, more
77-
sockets are opened until the minimum is reached.
130+
The following code creates a ``MongoClient`` instance with a maximum number of
131+
``2`` connections to be established concurrently per pool by specifying the
132+
``maxConnecting`` option in the ``options`` object:
133+
134+
.. code-block:: javascript
135+
136+
const { MongoClient } = require('mongodb');
137+
138+
const uri = '<connection-string>';
139+
const client = new MongoClient(uri, {
140+
maxConnecting: 2
141+
});
142+
143+
.. _node-connection-pool-min-pool-size:
144+
145+
minPoolSize
146+
~~~~~~~~~~~
147+
148+
You can set the minimum number of connections to each server with the
149+
``minPoolSize`` option. The driver ensures that there are always at least the
150+
number of connections set by the ``minPoolSize`` option in the connection pool.
151+
If sockets are closed, causing the total number of sockets (both in use and
152+
idle) to drop below the minimum, more sockets are opened until the minimum is
153+
reached.
154+
155+
The following code creates a ``MongoClient`` instance with a minimum connnection
156+
pool size of ``10`` by specifying the ``minPoolSize`` option in the ``options``
157+
object:
158+
159+
.. code-block:: javascript
160+
161+
const { MongoClient } = require('mongodb');
162+
163+
const uri = '<connection-string>';
164+
const client = new MongoClient(uri, {
165+
minPoolSize: 10
166+
});
167+
168+
.. _node-connection-pool-max-idle-time:
169+
170+
maxIdleTimeMS
171+
~~~~~~~~~~~~~
78172

79173
You can set the maximum number of milliseconds that a connection can
80174
remain idle in the pool by setting the ``maxIdleTimeMS`` option.
81175
Once a connection has been idle for ``maxIdleTimeMS``, the connection
82176
pool removes and replaces it. This option defaults to ``0`` (no limit).
83177

84-
The following default configuration for a ``MongoClient`` works for most
85-
applications:
178+
The following code creates a ``MongoClient`` instance with a maximum idle time
179+
of ``10000`` milliseconds (10 seconds) by specifying the ``maxIdleTimeMS``
180+
setting in the ``options`` object:
181+
182+
.. code-block:: javascript
183+
184+
const { MongoClient } = require('mongodb');
185+
186+
const uri = '<connection-string>';
187+
const client = new MongoClient(uri, {
188+
maxIdleTimeMS: 10000
189+
});
190+
191+
.. _node-connection-pool-wait-queue-timeout:
86192

87-
.. code-block:: js
88-
89-
const client = new MongoClient("<connection string>");
193+
waitQueueTimeoutMS
194+
~~~~~~~~~~~~~~~~~~
90195

91196
``MongoClient`` supports multiple concurrent requests. For each process,
92197
create a client and reuse it for all operations in a process. This
@@ -103,39 +208,72 @@ A request that waits more than the length of time defined by
103208
option if it is more important to bound the duration of operations
104209
during a load spike than it is to complete every operation.
105210

106-
When ``MongoClient.close()`` is called by any request, the driver
107-
closes all idle sockets and closes all sockets that are in
108-
use as they are returned to the pool. Calling ``MongoClient.close()``
109-
closes only inactive sockets and does not directly terminate
110-
any ongoing operations. The driver closes any in-use sockets only when
111-
the associated operations complete. However, the ``MongoClient.close()``
112-
method does close existing sessions and transactions, which might indirectly
113-
affect the behavior of ongoing operations and open cursors.
211+
The following code creates a ``MongoClient`` instance with a maximum wait queue
212+
timeout of ``10000`` milliseconds (10 seconds) by declaring it in the
213+
``options`` object:
214+
215+
.. code-block:: javascript
216+
217+
const { MongoClient } = require('mongodb');
218+
219+
const uri = '<connection-string>';
220+
const client = new MongoClient(uri, {
221+
waitQueueTimeoutMS: 10000
222+
});
223+
224+
Closing Connections
225+
--------------------
226+
227+
When any request calls ``MongoClient.close()``, the {+driver-short+} performs
228+
the following actions:
229+
230+
- Closes all idle sockets in the connection pool
231+
- Closes all sockets that are in use as they are returned to the pool
232+
- Closes all sockets that are in use only when the associated operations
233+
complete
234+
235+
Calling ``MongoClient.close()`` closes only inactive sockets and does not
236+
directly terminate any ongoing operations.
237+
238+
.. note::
239+
240+
The ``MongoClient.close()`` method does close existing sessions and
241+
transactions, which might indirectly affect the behavior of ongoing
242+
operations and open cursors.
114243

115244
Avoid Socket Timeouts
116245
---------------------
117246

118247
Having a large connection pool does not always reduce reconnection
119-
requests. Consider the following example:
248+
requests. Consider the following example scenario:
120249

121-
An application has a connection pool size of 5 sockets and has the
122-
``socketTimeoutMS`` option set to 5000 milliseconds. Operations occur,
123-
on average, every 3000 milliseconds, and reconnection requests are
124-
frequent. Each socket times out after 5000 milliseconds, which means
125-
that all sockets must do something during those 5000 milliseconds to
126-
avoid closing.
250+
- An application has a connection pool size of 5 sockets and has the
251+
``socketTimeoutMS`` option set to 5000 milliseconds.
252+
- Operations occur, on average, every 3000 milliseconds, and reconnection
253+
requests are frequent.
254+
- Each socket times out after 5000 milliseconds, which means that all sockets
255+
must do something during those 5000 milliseconds to avoid closing.
127256

128-
One message every 3000 milliseconds is not enough to keep the sockets
129-
active, so several of the sockets will time out after 5000 milliseconds.
130-
To avoid excessive socket timeouts, reduce the number of connections
131-
that the driver can maintain in the connection pool by specifying the
132-
``maxPoolSize`` option.
257+
In this scenario, each socket times out after 5000 milliseconds, requiring
258+
activity within this timeout period to avoid closure. However, one message every
259+
3000 milliseconds isn't enough to keep all sockets active, causing several of
260+
them to time out.
133261

134-
To specify the optional ``maxPoolSize`` setting for your ``MongoClient``, declare
135-
it in the ``options`` object of the constructor as follows:
262+
To avoid excessive socket timeouts, reduce the number of connections that the
263+
driver can maintain in the connection pool by specifying the ``maxPoolSize``
264+
option. To learn how to set the ``maxPoolSize`` option, see the
265+
:ref:`node-connection-pool-max-pool-size` section.
136266

137-
.. code-block:: javascript
267+
API Documentation
268+
-----------------
138269

139-
const client = new MongoClient(uri, {
140-
maxPoolSize: <integer value>,
141-
});
270+
For more information about creating a ``MongoClient`` object with the
271+
{+driver-short+} and specifying options, see the following API documentation:
272+
273+
- `MongoClient <{+api+}/classes/MongoClient.html>`__
274+
- `MongoClientOptions <{+api+}/interfaces/MongoClientOptions.html>`__
275+
- `maxPoolSize <{+api+}/interfaces/MongoClientOptions.html#maxPoolSize>`__
276+
- `maxConnecting <{+api+}/interfaces/MongoClientOptions.html#maxConnecting>`__
277+
- `minPoolSize <{+api+}/interfaces/MongoClientOptions.html#minPoolSize>`__
278+
- `maxIdleTimeMS <{+api+}/interfaces/MongoClientOptions.html#maxIdleTimeMS>`__
279+
- `waitQueueTimeoutMS <{+api+}/interfaces/MongoClientOptions.html#waitQueueTimeoutMS>`__

source/crud.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ CRUD Operations
3131
Compound Operations </crud/compound-operations>
3232
Transactions </crud/transactions>
3333
Configure CRUD Operations </crud/configure>
34-
Operations on Replica Sets </crud/read-write-pref>
3534
Store Large Files </crud/gridfs>
3635

3736
CRUD (Create, Read, Update, Delete) operations enable you to work with

0 commit comments

Comments
 (0)