|
| 1 | +=================== |
| 2 | +Connection Settings |
| 3 | +=================== |
| 4 | + |
| 5 | +This section describes common MongoDB client connection settings for the |
| 6 | +Node.js driver. These settings provide additional control over the negotiation |
| 7 | +of the connection and the behavior of the client, once a connection is |
| 8 | +established. |
| 9 | + |
| 10 | +The connection settings are passed as an optional parameter to the |
| 11 | +``MongoClient`` as shown in the code sample below: |
| 12 | + |
| 13 | +.. code-block:: js |
| 14 | + |
| 15 | + const { MongoClient } = require("mongodb"); |
| 16 | + |
| 17 | + // Connection URI |
| 18 | + const uri = `mongodb+srv://<username>:<password>@<clusterUrl>`; |
| 19 | + |
| 20 | + // Replace this with your constructor options |
| 21 | + const opts = { |
| 22 | + loggerLevel: "info", |
| 23 | + useUnifiedTopology: true, |
| 24 | + }; |
| 25 | + |
| 26 | + // Create a new MongoClient |
| 27 | + const client = new MongoClient(uri, opts); |
| 28 | + |
| 29 | + async function run() { |
| 30 | + try { |
| 31 | + // Connect the client to the server |
| 32 | + await client.connect(); |
| 33 | + |
| 34 | + // Establish and verify connection |
| 35 | + await client.db("admin").command({ ping: 1 }); |
| 36 | + console.log("Connected successfully to server"); |
| 37 | + } finally { |
| 38 | + // Ensures that the client will close when you finish/error |
| 39 | + await client.close(); |
| 40 | + } |
| 41 | + } |
| 42 | + run().catch(console.dir); |
| 43 | + |
| 44 | +The following table describes several common connection options that can be |
| 45 | +passed to the ``MongoClient`` constructor. |
| 46 | + |
| 47 | +.. list-table:: |
| 48 | + :header-rows: 1 |
| 49 | + |
| 50 | + * - Option Name |
| 51 | + - Type |
| 52 | + - Default Value |
| 53 | + - Description |
| 54 | + * - **useUnifiedTopology** |
| 55 | + - boolean |
| 56 | + - **false** |
| 57 | + - Specifies whether to opt into the new Server Discovery and Monitoring |
| 58 | + (SDAM) engine. The next major driver release removes the old engine and |
| 59 | + deprecates this option. |
| 60 | + * - **validateOptions** |
| 61 | + - boolean |
| 62 | + - **false** |
| 63 | + - Specifies whether to error when instantiating the client the constructor |
| 64 | + is passed an unknown option. If ``false``, the driver produces warnings |
| 65 | + only. |
| 66 | + * - **poolSize** |
| 67 | + - integer |
| 68 | + - **5** |
| 69 | + - Specifies the maximum size of the individual server connection pool. |
| 70 | + * - **minSize** |
| 71 | + - integer |
| 72 | + - **0** |
| 73 | + - Specifies the minimum size of connections to maintain in the individual |
| 74 | + server pool. |
| 75 | + * - **tlsInsecure** |
| 76 | + - boolean |
| 77 | + - **false** |
| 78 | + - Specifies whether to skip validation of the server certificate with the |
| 79 | + Certificate Authority. |
| 80 | + * - **family** |
| 81 | + - number |
| 82 | + - **null** |
| 83 | + - Specifies the version of the IP stack. Possible values include: **4**, |
| 84 | + **6**, **0**, or **null**. The **0** and **null** settings attempt to |
| 85 | + connect with IPv6 and fall back to IPv4 upon failure. |
| 86 | + * - **tlsCAFile** |
| 87 | + - string |
| 88 | + - **null** |
| 89 | + - Specifies the path to a file containing one or more certificate |
| 90 | + authorities to trust when establishing a TLS connection. |
| 91 | + * - **tlsCertificateFile** |
| 92 | + - string |
| 93 | + - **null** |
| 94 | + - Specifies the path to the client certificate file or the client private |
| 95 | + key file. If both are required, the two files should be concatenated. |
| 96 | + * - **tlsCertificateKeyFilePassword** |
| 97 | + - string |
| 98 | + - **null** |
| 99 | + - Specifies the password to decrypt client private key used for TLS |
| 100 | + connections. |
| 101 | + * - **noDelay** |
| 102 | + - boolean |
| 103 | + - **true** |
| 104 | + - Specifies whether to use the TCP socket no delay option. For more |
| 105 | + information, see the documentation for `Node.js socket.setNoDelay |
| 106 | + <https://nodejs.org/dist/latest-v10.x/docs/api/net.html#net_socket_setnodelay_nodelay>`_. |
| 107 | + * - **keepAlive** |
| 108 | + - boolean |
| 109 | + - **true** |
| 110 | + - Specifies whether ``keepAlive`` is enabled on the TCP socket. For more |
| 111 | + information, see the documentation for `Node.js socket.setKeepAlive |
| 112 | + <https://nodejs.org/dist/latest-v10.x/docs/api/net.html#net_socket_setkeepalive_enable_initialdelay>`_. |
| 113 | + * - **keepAliveInitialDelay** |
| 114 | + - integer |
| 115 | + - **30000** |
| 116 | + - Specifies the number of milliseconds to wait before initiating |
| 117 | + ``keepAlive`` on the TCP socket. For more information, see the |
| 118 | + documentation for `Node.js socket.setKeepAlive |
| 119 | + <https://nodejs.org/dist/latest-v10.x/docs/api/net.html#net_socket_setkeepalive_enable_initialdelay>`_. |
| 120 | + * - **connectTimeoutMS** |
| 121 | + - integer |
| 122 | + - **30000** |
| 123 | + - Specifies the number of milliseconds to wait before timeout on a TCP |
| 124 | + connection. |
| 125 | + * - **socketTimeoutMS** |
| 126 | + - integer |
| 127 | + - **360000** |
| 128 | + - Specifies the number of milliseconds to wait before timeout on a TCP |
| 129 | + socket. |
| 130 | + * - **w** |
| 131 | + - string or integer |
| 132 | + - **null** |
| 133 | + - Specifies the write concern. For more information on values, see the |
| 134 | + server documentation on the |
| 135 | + :manual:`w Option </reference/write-concern/#w-option>`. |
| 136 | + * - **forceServerObjectId** |
| 137 | + - boolean |
| 138 | + - **false** |
| 139 | + - Specifies whether to force the server to assign ``_id`` values to |
| 140 | + documents instead of the driver. |
| 141 | + * - **serializeFunctions** |
| 142 | + - boolean |
| 143 | + - **false** |
| 144 | + - Specifies whether to serialize functions on any object passed to the |
| 145 | + server. |
| 146 | + * - **ignoreUndefined** |
| 147 | + - boolean |
| 148 | + - **false** |
| 149 | + - Specifies whether the BSON serializer should ignore undefined fields. |
| 150 | + * - **raw** |
| 151 | + - boolean |
| 152 | + - **false** |
| 153 | + - Specifies whether to return document results as raw BSON buffers. |
| 154 | + * - **promoteLongs** |
| 155 | + - boolean |
| 156 | + - **true** |
| 157 | + - Specifies whether to convert ``Long`` values to a number if they fit |
| 158 | + inside 53 bits of resolution. |
| 159 | + * - **promoteBuffers** |
| 160 | + - boolean |
| 161 | + - **false** |
| 162 | + - Specifies whether to promote Binary BSON values to native Node.js |
| 163 | + ``Buffer`` type data. |
| 164 | + * - **promoteValues** |
| 165 | + - boolean |
| 166 | + - **true** |
| 167 | + - Specifies whether to promote BSON values to Node.js native types when |
| 168 | + possible. When set to false, BSON values are presented as wrapper types. |
| 169 | + * - **pkFactory** |
| 170 | + - object |
| 171 | + - **null** |
| 172 | + - Specifies a primary key factory object that generates custom ``_id`` |
| 173 | + keys. |
| 174 | + * - **promiseLibrary** |
| 175 | + - object |
| 176 | + - **null** |
| 177 | + - Specifies the Promise library class the application uses (e.g. Bluebird). |
| 178 | + This library must be compatible with ES6. |
| 179 | + * - **loggerLevel** |
| 180 | + - string |
| 181 | + - **null** |
| 182 | + - Specifies the logger level used by the driver. Valid choices are: |
| 183 | + ``error``, ``warn``, ``info``, and ``debug``. |
| 184 | + * - **logger** |
| 185 | + - object |
| 186 | + - **null** |
| 187 | + - Specifies a custom logger to be used by the client. |
| 188 | + |
| 189 | +For a complete list of options, see the :node-api:`MongoClient |
| 190 | +<MongoClient.html>` API reference page. |
0 commit comments