Skip to content

Commit cb9d915

Browse files
authoredAug 2, 2018
docs(connect): remove references to MongoClient.connect
We want to encourage people to manually create a new MongoClient, and then call connect, instead of using the static method MongoClient.connect Fixes NODE-1585
1 parent b8d2f1d commit cb9d915

16 files changed

+174
-122
lines changed
 

‎docs/reference/content/quick-start/quick-start.md

+13-8
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,10 @@ const url = 'mongodb://localhost:27017';
106106

107107
// Database Name
108108
const dbName = 'myproject';
109+
const client = new MongoClient(url);
109110

110111
// Use connect method to connect to the server
111-
MongoClient.connect(url, function(err, client) {
112+
client.connect(url, function(err) {
112113
assert.equal(null, err);
113114
console.log("Connected successfully to server");
114115

@@ -156,7 +157,7 @@ rather than from within a browser console, and console.log returns better format
156157
-->
157158

158159

159-
This query returns all the documents in the **documents** collection. Add the **findDocument** method to the **MongoClient.connect** callback:
160+
This query returns all the documents in the **documents** collection. Add the **findDocument** method to the **client.connect** callback:
160161

161162
<!---
162163
Removed the assert line for number of documents returned on the grounds that it's too brittle.
@@ -174,8 +175,9 @@ const url = 'mongodb://localhost:27017';
174175
// Database Name
175176
const dbName = 'myproject';
176177

178+
const client = new MongoClient(url);
177179
// Use connect method to connect to the server
178-
MongoClient.connect(url, function(err, client) {
180+
client.connect(function(err) {
179181
assert.equal(null, err);
180182
console.log("Connected correctly to server");
181183

@@ -229,7 +231,7 @@ const updateDocument = function(db, callback) {
229231
}
230232
```
231233

232-
The method updates the first document where the field **a** is equal to **2** by adding a new field **b** to the document set to **1**. Next, update the callback function from **MongoClient.connect** to include the update method.
234+
The method updates the first document where the field **a** is equal to **2** by adding a new field **b** to the document set to **1**. Next, update the callback function from **client.connect** to include the update method.
233235

234236
```js
235237
const MongoClient = require('mongodb').MongoClient;
@@ -241,8 +243,9 @@ const url = 'mongodb://localhost:27017';
241243
// Database Name
242244
const dbName = 'myproject';
243245

246+
const client = new MongoClient(url);
244247
// Use connect method to connect to the server
245-
MongoClient.connect(url, function(err, client) {
248+
client.connect(function(err) {
246249
assert.equal(null, err);
247250
console.log("Connected successfully to server");
248251

@@ -274,7 +277,7 @@ const removeDocument = function(db, callback) {
274277
}
275278
```
276279

277-
Add the new method to the **MongoClient.connect** callback function.
280+
Add the new method to the **client.connect** callback function.
278281

279282
```js
280283
const MongoClient = require('mongodb').MongoClient;
@@ -286,8 +289,9 @@ const url = 'mongodb://localhost:27017';
286289
// Database Name
287290
const dbName = 'myproject';
288291

292+
const client = new MongoClient(url);
289293
// Use connect method to connect to the server
290-
MongoClient.connect(url, function(err, client) {
294+
client.connect(function(err) {
291295
assert.equal(null, err);
292296
console.log("Connected successfully to server");
293297

@@ -325,8 +329,9 @@ const url = 'mongodb://localhost:27017';
325329

326330
const dbName = 'myproject';
327331

332+
const client = new MongoClient(url);
328333
// Use connect method to connect to the server
329-
MongoClient.connect(url, function(err, client) {
334+
client.connect(function(err) {
330335
assert.equal(null, err);
331336
console.log("Connected successfully to server");
332337

‎docs/reference/content/reference/connecting/connection-settings.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ title = "Connection Settings"
1010

1111
# URI Connection Settings
1212

13-
Optional connection settings are settings not covered by the [URI Connection String ](https://docs.mongodb.org/manual/reference/connection-string/). The following options are passed in the options parameter in the MongoClient.connect function.
13+
Optional connection settings are settings not covered by the [URI Connection String ](https://docs.mongodb.org/manual/reference/connection-string/). The following options are passed in the options parameter when you create a mongo client.
1414

1515
```js
1616
const MongoClient = require('mongodb').MongoClient;
@@ -20,11 +20,14 @@ const assert = require('assert');
2020
const url = 'mongodb://localhost:50000,localhost:50001';
2121
// Database Name
2222
const dbName = 'myproject';
23-
// Use connect method to connect to the Server passing in
24-
// additional options
25-
MongoClient.connect(url, {
23+
24+
// create a client, passing in additional options
25+
const client = new MongoClient(url, {
2626
poolSize: 10, ssl: true
27-
}, function(err, client) {
27+
});
28+
29+
// Use connect method to connect to the server
30+
client.connect(function(err) {
2831
assert.equal(null, err);
2932
console.log("Connected correctly to server");
3033

‎docs/reference/content/reference/ecmascriptnext/connecting.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,19 @@ const assert = require('assert');
2121
const url = 'mongodb://localhost:27017/myproject';
2222
// Database Name
2323
const dbName = 'myproject';
24-
let client;
24+
const client = new MongoClient(url);
2525

2626
try {
2727
// Use connect method to connect to the Server
28-
client = await MongoClient.connect(url);
28+
await client.connect();
2929

3030
const db = client.db(dbName);
3131
} catch (err) {
3232
console.log(err.stack);
3333
}
3434

35-
if (client) {
36-
client.close();
37-
}
35+
client.close();
3836
})();
3937
```
4038

41-
The `MongoClient.connect` function returns a `Promise` that we then execute using the `await` keyword inside of an `async` function. If an error happens during the `MongoClient.connect` the error is caught by the `try`/`catch` and can be handled as if it were a normal Javascript error.
39+
The `client.connect` function returns a `Promise` that we then execute using the `await` keyword inside of an `async` function. If an error happens during the `client.connect` the error is caught by the `try`/`catch` and can be handled as if it were a normal Javascript error.

‎docs/reference/content/reference/ecmascriptnext/crud.md

+22-22
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ const url = 'mongodb://localhost:27017';
2727
const dbName = 'myproject';
2828

2929
(async function() {
30-
let client;
30+
const client = new MongoClient(url);
3131

3232
try {
33-
client = await MongoClient.connect(url);
33+
await client.connect();
3434
console.log("Connected correctly to server");
3535

3636
const db = client.db(dbName);
@@ -61,10 +61,10 @@ const url = 'mongodb://localhost:27017';
6161
const dbName = 'myproject';
6262

6363
(async function() {
64-
let client;
64+
const client = new MongoClient(url);
6565

6666
try {
67-
client = await MongoClient.connect(url);
67+
await client.connect();
6868
console.log("Connected correctly to server");
6969

7070
const db = client.db(dbName);
@@ -104,10 +104,10 @@ const url = 'mongodb://localhost:27017';
104104
const dbName = 'myproject';
105105

106106
(async function() {
107-
let client;
107+
const client = new MongoClient(url);
108108

109109
try {
110-
client = await MongoClient.connect(url);
110+
await client.connect();
111111
console.log("Connected correctly to server");
112112

113113
const db = client.db(dbName);
@@ -152,10 +152,10 @@ const url = 'mongodb://localhost:27017';
152152
const dbName = 'myproject';
153153

154154
(async function() {
155-
let client;
155+
const client = new MongoClient(url);
156156

157157
try {
158-
client = await MongoClient.connect(url);
158+
await client.connect();
159159
console.log("Connected correctly to server");
160160

161161
const db = client.db(dbName);
@@ -195,10 +195,10 @@ const url = 'mongodb://localhost:27017';
195195
const dbName = 'myproject';
196196

197197
(async function() {
198-
let client;
198+
const client = new MongoClient(url);
199199

200200
try {
201-
client = await MongoClient.connect(url);
201+
await client.connect();
202202
console.log("Connected correctly to server");
203203

204204
const db = client.db(dbName);
@@ -238,10 +238,10 @@ const url = 'mongodb://localhost:27017';
238238
const dbName = 'myproject';
239239

240240
(async function() {
241-
let client;
241+
const client = new MongoClient(url);
242242

243243
try {
244-
client = await MongoClient.connect(url);
244+
await client.connect();
245245
console.log("Connected correctly to server");
246246

247247
const db = client.db(dbName);
@@ -277,10 +277,10 @@ const url = 'mongodb://localhost:27017';
277277
const dbName = 'myproject';
278278

279279
(async function() {
280-
let client;
280+
const client = new MongoClient(url);
281281

282282
try {
283-
client = await MongoClient.connect(url);
283+
await client.connect();
284284
console.log("Connected correctly to server");
285285

286286
const db = client.db(dbName);
@@ -327,10 +327,10 @@ const url = 'mongodb://localhost:27017';
327327
const dbName = 'myproject';
328328

329329
(async function() {
330-
let client;
330+
const client = new MongoClient(url);
331331

332332
try {
333-
client = await MongoClient.connect(url);
333+
await client.connect();
334334
console.log("Connected correctly to server");
335335

336336
const db = client.db(dbName);
@@ -417,10 +417,10 @@ const url = 'mongodb://localhost:27017';
417417
const dbName = 'myproject';
418418

419419
(async function() {
420-
let client;
420+
const client = new MongoClient(url);
421421

422422
try {
423-
client = await MongoClient.connect(url);
423+
await client.connect();
424424
console.log("Connected correctly to server");
425425

426426
const db = client.db(dbName);
@@ -454,10 +454,10 @@ const url = 'mongodb://localhost:27017';
454454
const dbName = 'myproject';
455455

456456
(async function() {
457-
let client;
457+
const client = new MongoClient(url);
458458

459459
try {
460-
client = await MongoClient.connect(url);
460+
await client.connect();
461461
console.log("Connected correctly to server");
462462

463463
const db = client.db(dbName);
@@ -497,10 +497,10 @@ const url = 'mongodb://localhost:27017';
497497
const dbName = 'myproject';
498498

499499
(async function() {
500-
let client;
500+
const client = new MongoClient(url);
501501

502502
try {
503-
client = await MongoClient.connect(url);
503+
await client.connect();
504504
console.log("Connected correctly to server");
505505

506506
const db = client.db(dbName);

‎docs/reference/content/reference/faq/index.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ title = "Frequently Asked Questions"
1010

1111
# What is the difference between connectTimeoutMS, socketTimeoutMS and maxTimeMS ?
1212

13-
| Setting | Default Value MongoClient.connect | Description |
13+
| Setting | Default Value client.connect | Description |
1414
| :----------| :------------- | :------------- |
1515
| connectTimeoutMS | 30000 | The connectTimeoutMS sets the number of milliseconds a socket stays inactive before closing during the connection phase of the driver. That is to say, when the application initiates a connection, when a replica set connects to new members, or when a replica set reconnects to members. A value of 10000 milliseconds would mean the driver would wait up to 10 seconds for a response from a MongoDB server.|
1616
| socketTimeoutMS | 360000 | The socketTimeoutMS sets the number of milliseconds a socket stays inactive after the driver has successfully connected before closing. If the value is set to 360000 milliseconds, the socket closes if there is no activity during a 6 minutes window.|
@@ -106,12 +106,13 @@ are some things to check:
106106
allowing the driver to detect that the socket is closed.
107107
2. The firewall should allow keepAlive probes.
108108

109-
# I'm getting ECONNRESET when calling MongoClient.connect
109+
# I'm getting ECONNRESET when calling client.connect
110110
This can occur if the connection pool is too large.
111111

112112
```js
113-
MongoClient.connect('mongodb://localhost:27017/test?maxPoolSize=5000',
114-
function(err, client) {
113+
const client = new MongoClient('mongodb://localhost:27017/test?maxPoolSize=5000');
114+
client.connect('mongodb://localhost:27017/test?maxPoolSize=5000',
115+
function(err) {
115116
// connection
116117
});
117118
```

‎docs/reference/content/reference/management/logging.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ const url = 'mongodb://localhost:27017';
2828
// Database Name
2929
const dbName = 'myprojeect';
3030

31-
// Use connect method to connect to the Server
32-
MongoClient.connect(url, function(err, client) {
31+
const client = new MongoClient(url);
32+
// Use connect method to connect to the server
33+
client.connect(function(err) {
3334
assert.equal(null, err);
3435
console.log("Connected correctly to server");
3536

@@ -61,8 +62,9 @@ const url = 'mongodb://localhost:27017';
6162
// Database Name
6263
const dbName = 'myprojeect';
6364

64-
// Use connect method to connect to the Server
65-
MongoClient.connect(url, function(err, client) {
65+
const client = new MongoClient(url);
66+
// Use connect method to connect to the server
67+
client.connect(function(err) {
6668
assert.equal(null, err);
6769
console.log("Connected correctly to server");
6870

@@ -129,8 +131,9 @@ const url = 'mongodb://localhost:27017';
129131
// Database Name
130132
const dbName = 'myprojeect';
131133

132-
// Use connect method to connect to the Server
133-
MongoClient.connect(url, function(err, client) {
134+
const client = new MongoClient(url);
135+
// Use connect method to connect to the server
136+
client.connect(function(err) {
134137
assert.equal(null, err);
135138
console.log("Connected correctly to server");
136139

‎docs/reference/content/tutorials/connect/authenticating.md

+36-23
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,20 @@ The user and password should always be **URI** encoded using `encodeURIComponent
3131

3232
```js
3333
const MongoClient = require('mongodb').MongoClient;
34-
const f = require('util').format;
3534
const assert = require('assert');
3635

3736
const user = encodeURIComponent('dave');
3837
const password = encodeURIComponent('abc123');
3938
const authMechanism = 'DEFAULT';
4039

4140
// Connection URL
42-
const url = f('mongodb://%s:%s@localhost:27017/?authMechanism=%s',
43-
user, password, authMechanism);
41+
const url = `mongodb://${user}:${password}@localhost:27017/?authMechanism=${authMechanism}`;
42+
43+
// Create a new MongoClient
44+
const client = new MongoClient(url);
4445

4546
// Use connect method to connect to the Server
46-
MongoClient.connect(url, function(err, client) {
47+
client.connect(function(err) {
4748
assert.equal(null, err);
4849
console.log("Connected correctly to server");
4950

@@ -61,13 +62,16 @@ In the following example, the connection string specifies the user `dave`, passw
6162

6263
```js
6364
const MongoClient = require('mongodb').MongoClient;
64-
const f = require('util').format;
6565
const assert = require('assert');
6666

6767
// Connection URL
6868
const url = 'mongodb://dave:abc123@localhost:27017/?authMechanism=SCRAM-SHA-1&authSource=myprojectdb';
69+
70+
// Create a new MongoClient
71+
const client = new MongoClient(url);
72+
6973
// Use connect method to connect to the Server
70-
MongoClient.connect(url, function(err, client) {
74+
client.connect(function(err) {
7175
assert.equal(null, err);
7276
console.log("Connected correctly to server");
7377

@@ -90,13 +94,16 @@ In the following example, the connection string specifies the user `dave`, passw
9094

9195
```js
9296
const MongoClient = require('mongodb').MongoClient;
93-
const f = require('util').format;
9497
const assert = require('assert');
9598

9699
// Connection URL
97100
const url = 'mongodb://dave:abc123@localhost:27017/?authMechanism=MONGODB-CR&authSource=myprojectdb';
101+
102+
// Create a new MongoClient
103+
const client = new MongoClient(url);
104+
98105
// Use connect method to connect to the Server
99-
MongoClient.connect(url, function(err, client) {
106+
client.connect(function(err) {
100107
assert.equal(null, err);
101108
console.log("Connected correctly to server");
102109

@@ -119,12 +126,11 @@ X.509 authentication requires the use of SSL connections with certificate valida
119126

120127
To connect using the X.509 authentication mechanism, specify `MONGODB-X509` as the mechanism in the [URI connection string](https://docs.mongodb.org/manual/reference/connection-string/), `ssl=true`, and the username. Use `enodeURIComponent` to encode the username string.
121128

122-
In addition to the connection string, pass to the `MongoClient.connect` method a connections options for the `server` with the X.509 certificate and other [TLS/SSL connections]({{< relref "tutorials/connect/ssl.md" >}}) options.
129+
In addition to the connection string, pass to the new `MongoClient` a connections options for the `server` with the X.509 certificate and other [TLS/SSL connections]({{< relref "tutorials/connect/ssl.md" >}}) options.
123130

124131

125132
```js
126133
const MongoClient = require('mongodb').MongoClient;
127-
const f = require('util').format;
128134
const assert = require('assert');
129135

130136
// Read the cert and key
@@ -133,15 +139,17 @@ const key = fs.readFileSync(__dirname + "/ssl/x509/client.pem");
133139

134140
// User name
135141
const userName = encodeURIComponent("CN=client,OU=kerneluser,O=10Gen,L=New York City,ST=New York,C=US");
142+
const url = `mongodb://${userName}:${password}@server:27017?authMechanism=MONGODB-X509&ssl=true`;
136143

137-
// Connect using X509 authentication
138-
MongoClient.connect(f('mongodb://%s@server:27017?authMechanism=MONGODB-X509&ssl=true', userName), {
139-
server: {
140-
sslKey:key
141-
, sslCert:cert
142-
, sslValidate:false
143-
}
144-
}, function(err, client) {
144+
// Create a new MongoClient
145+
const client = new MongoClient(url, {
146+
sslKey: key,
147+
sslCert: cert,
148+
sslValidate: false
149+
});
150+
151+
// Use connect method to connect to the Server
152+
client.connect(function(err) {
145153
assert.equal(null, err);
146154
console.log("Connected correctly to server");
147155

@@ -164,16 +172,19 @@ The following example connects to MongoDB using Kerberos for UNIX.
164172

165173
```js
166174
const MongoClient = require('mongodb').MongoClient;
167-
const f = require('util').format;
168175
const assert = require('assert');
169176

170177
// KDC Server
171178
const server = "mongo-server.example.com";
172179
const principal = "drivers@KERBEROS.EXAMPLE.COM";
173180
const urlEncodedPrincipal = encodeURIComponent(principal);
174181

182+
const url = `mongodb://${urlEncodedPrincipal}@${server}?authMechanism=GSSAPI&gssapiServiceName=mongodb`;
183+
184+
const client = new MongoClient(url);
185+
175186
// Let's write the actual connection code
176-
MongoClient.connect(f("mongodb://%s@%s?authMechanism=GSSAPI&gssapiServiceName=mongodb", urlEncodedPrincipal, server), function(err, client) {
187+
client.connect(function(err) {
177188
assert.equal(null, err);
178189

179190
client.close();
@@ -193,7 +204,6 @@ To connect using the LDAP authentication mechanism, specify ``authMechanism=PLAI
193204

194205
```js
195206
const MongoClient = require('mongodb').MongoClient;
196-
const f = require('util').format;
197207
const assert = require('assert');
198208

199209
// LDAP Server
@@ -202,10 +212,13 @@ const user = "ldap-user";
202212
const pass = "ldap-password";
203213

204214
// Url
205-
const url = f("mongodb://%s:%s@%s?authMechanism=PLAIN&maxPoolSize=1", user, pass, server);
215+
const url = `mongodb://${user}:${pass}@${server}?authMechanism=PLAIN&maxPoolSize=1`;
216+
217+
// Client
218+
const client = new MongoClient(url);
206219

207220
// Let's write the actual connection code
208-
MongoClient.connect(url, function(err, client) {
221+
client.connect(function(err) {
209222
assert.equal(null, err);
210223

211224
client.close();

‎docs/reference/content/tutorials/connect/index.md

+10-11
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ title = "Connect to MongoDB"
1010

1111
# Connect to MongoDB
1212

13-
Use the `MongoClient.connect` method to connect to a running MongoDB deployment.
13+
Use the `client.connect` method to connect to a running MongoDB deployment.
1414

1515
## Connect to a Single MongoDB Instance
1616

@@ -68,7 +68,6 @@ For example, you can specify TLS/SSL and authentication setting.
6868
```js
6969

7070
const MongoClient = require('mongodb').MongoClient;
71-
const f = require('util').format;
7271
const assert = require('assert');
7372
const fs = require('fs');
7473

@@ -79,15 +78,15 @@ const cert = fs.readFileSync(__dirname + "/ssl/client.pem");
7978
// Connection URL
8079
const url = 'mongodb://dave:password@localhost:27017?authMechanism=DEFAULT&authSource=db&ssl=true"';
8180

82-
// Use connect method to connect to the Server passing in
83-
// additional options
84-
MongoClient.connect(url, {
85-
server: {
86-
sslValidate:true
87-
, sslCA:ca
88-
, sslCert:cert
89-
}
90-
}, function(err, client) {
81+
// Create a client, passing in additional options
82+
const client = new MongoClient(url, {
83+
sslValidate: true,
84+
sslCA: ca,
85+
sslCert: cert
86+
});
87+
88+
// Use connect method to connect to the server
89+
client.connect(function(err) {
9190
assert.equal(null, err);
9291
console.log("Connected correctly to server");
9392

‎docs/reference/content/tutorials/connect/ssl.md

+39-28
Original file line numberDiff line numberDiff line change
@@ -18,87 +18,92 @@ If the MongoDB instance does not perform any validation of the certificate chain
1818
```js
1919
const MongoClient = require('mongodb').MongoClient;
2020

21-
MongoClient.connect("mongodb://localhost:27017?ssl=true", function(err, client) {
21+
const client = new MongoClient('mongodb://localhost:27017?ssl=true');
22+
23+
client.connect(function(err) {
2224
client.close();
2325
});
2426
```
2527

2628
## Validate Server Certificate
27-
If the MongoDB instance presents a certificate, to validate the server's certificate, pass to the `MongoClient.connect` method:
29+
If the MongoDB instance presents a certificate, to validate the server's certificate, pass the following when creating a `MongoClient`:
2830

2931
- A [URI Connection String ](https://docs.mongodb.org/manual/reference/connection-string/) that includes `ssl=true` setting,
3032

3133
- A connections options with the certificate for the Certificate Authority (`sslCA`) and the `sslValidate` setting set to `true`
3234

3335
```js
3436
const MongoClient = require('mongodb').MongoClient;
35-
const f = require('util').format;
3637
const fs = require('fs');
3738

3839
// Read the certificate authority
3940
const ca = [fs.readFileSync(__dirname + "/ssl/ca.pem")];
4041

41-
// Connect validating the returned certificates from the server
42-
MongoClient.connect("mongodb://localhost:27017?ssl=true", {
42+
const client = new MongoClient('mongodb://localhost:27017?ssl=true', {
4343
sslValidate:true,
4444
sslCA:ca
45-
}, function(err, client) {
45+
});
46+
47+
// Connect validating the returned certificates from the server
48+
client.connect(function(err) {
4649
client.close();
4750
});
4851
```
4952

5053
## Disable Hostname Verification
5154
By default, the driver ensures that the hostname included in the
52-
server's SSL certificate(s) matches the hostname(s) provided in the URI connection string. If you need to disable the hostname verification, but otherwise validate the server's certificate, pass to the `MongoClient.connect` method:
55+
server's SSL certificate(s) matches the hostname(s) provided in the URI connection string. If you need to disable the hostname verification, but otherwise validate the server's certificate, pass to the new `MongoClient`:
5356

5457
- A [URI Connection String ](https://docs.mongodb.org/manual/reference/connection-string/) that includes `ssl=true` setting,
5558

5659
- A connections options with the certificate for the Certificate Authority (`sslCA`) and the `sslValidate` setting set to `true` but `checkServerIdentity` set to `false`.
5760

5861
```js
5962
const MongoClient = require('mongodb').MongoClient;
60-
const f = require('util').format;
6163
const fs = require('fs');
6264

6365
// Read the certificate authority
6466
const ca = [fs.readFileSync(__dirname + "/ssl/ca.pem")];
6567

66-
// Connect validating the returned certificates from the server
67-
MongoClient.connect("mongodb://localhost:27017?ssl=true", {
68+
const client = new MongoClient('mongodb://localhost:27017?ssl=true', {
6869
sslValidate:true,
6970
checkServerIdentity:false,
7071
sslCA:ca
71-
}, function(err, client) {
72+
});
73+
74+
// Connect validating the returned certificates from the server
75+
client.connect(function(err) {
7276
client.close();
7377
});
7478
```
7579

7680
## Validate Server Certificate and Present Valid Certificate
7781
If the MongoDB server performs certificate validation, the client must pass its
78-
certificate to the server. To pass the client's certificate as well as to validate the server's certificate, pass to the `MongoClient.connect` method:
82+
certificate to the server. To pass the client's certificate as well as to validate the server's certificate, pass to the new `MongoClient`:
7983

8084
- A [URI Connection String ](https://docs.mongodb.org/manual/reference/connection-string/) that includes `ssl=true` setting,
8185

8286
- A connections options with the `sslValidate` setting set to `true`, the certificate for the Certificate Authority (`sslCA`), the client's certificate (`sslCert`) and private key file (`sslKey`). If the client's key file is encrypted, include the password (`sslPass`).
8387

8488
```js
8589
const MongoClient = require('mongodb').MongoClient;
86-
const f = require('util').format;
8790
const fs = require('fs');
8891

8992
// Read the certificates
9093
const ca = [fs.readFileSync(__dirname + "/ssl/ca.pem")];
9194
const cert = fs.readFileSync(__dirname + "/ssl/client.pem");
9295
const key = fs.readFileSync(__dirname + "/ssl/client.pem");
9396

94-
// Connect validating the returned certificates from the server
95-
MongoClient.connect("mongodb://localhost:27017?ssl=true", {
97+
const client = new MongoClient('mongodb://localhost:27017?ssl=true', {
9698
sslValidate:true,
9799
sslCA:ca,
98100
sslKey:key,
99101
sslCert:cert,
100102
sslPass:'10gen',
101-
}, function(err, client) {
103+
});
104+
105+
// Connect validating the returned certificates from the server
106+
client.connect(function(err) {
102107
client.close();
103108
});
104109
```
@@ -108,12 +113,11 @@ MongoClient.connect("mongodb://localhost:27017?ssl=true", {
108113

109114
To connect using the X.509 authentication mechanism, specify `MONGODB-X509` as the mechanism in the [URI connection string](https://docs.mongodb.org/manual/reference/connection-string/), `ssl=true`, and the username. Use `enodeURIComponent` to encode the username string.
110115

111-
In addition to the connection string, pass to the `MongoClient.connect` method
116+
In addition to the connection string, pass to the new `MongoClient`
112117
a connections options with the X.509 certificate and other [TLS/SSL connections]({{< relref "reference/connecting/connection-settings.md" >}}) options.
113118

114119
```js
115120
const MongoClient = require('mongodb').MongoClient;
116-
const f = require('util').format;
117121
const fs = require('fs');
118122

119123
// Read the cert and key
@@ -123,12 +127,13 @@ const key = fs.readFileSync(__dirname + "/ssl/x509/client.pem");
123127
// User name
124128
const userName = "CN=client,OU=kerneluser,O=10Gen,L=New York City,ST=New York,C=US";
125129

126-
// Connect using the MONGODB-X509 authentication mechanism
127-
MongoClient.connect(f('mongodb://%s@server:27017?authMechanism=%s&ssl=true'
128-
, encodeURIComponent(userName), 'MONGODB-X509'), {
130+
const client = new MongoClient(`mongodb://${encodeURIComponent(userName)}@server:27017?authMechanism=MONGODB-X509&ssl=true`, {
129131
sslKey:key,
130132
sslCert:cert,
131-
}, function(err, client) {
133+
});
134+
135+
// Connect using the MONGODB-X509 authentication mechanism
136+
client.connect(function(err) {
132137
client.close();
133138
});
134139
```
@@ -157,11 +162,13 @@ const ca = [fs.readFileSync(__dirname + "/ssl/ca.pem")];
157162
const cert = fs.readFileSync(__dirname + "/ssl/client.pem");
158163
const key = fs.readFileSync(__dirname + "/ssl/client.pem");
159164

160-
MongoClient.connect('mongodb://server:27017?ssl=true', {
165+
const client = new MongoClient('mongodb://server:27017?ssl=true', {
161166
sslCA:ca,
162167
sslKey:key,
163168
sslCert:cert,
164-
}, function(err, client) {
169+
});
170+
171+
client.connect(function(err) {
165172
client.close();
166173
});
167174
```
@@ -177,11 +184,13 @@ const ca = [fs.readFileSync(__dirname + "/ssl/ca.pem")];
177184
const cert = fs.readFileSync(__dirname + "/ssl/client.pem");
178185
const key = fs.readFileSync(__dirname + "/ssl/client.pem");
179186

180-
MongoClient.connect('mongodb://server:27017?replicaSet=foo&ssl=true', {
187+
const client = new MongoClient('mongodb://server:27017?replicaSet=foo&ssl=true', {
181188
sslCA:ca,
182189
sslKey:key,
183190
sslCert:cert,
184-
}, function(err, client) {
191+
});
192+
193+
client.connect(function(err) {
185194
client.close();
186195
});
187196
```
@@ -197,11 +206,13 @@ const ca = [fs.readFileSync(__dirname + "/ssl/ca.pem")];
197206
const cert = fs.readFileSync(__dirname + "/ssl/client.pem");
198207
const key = fs.readFileSync(__dirname + "/ssl/client.pem");
199208

200-
MongoClient.connect('mongodb://server:27017?ssl=true', {
209+
const client = new MongoClient('mongodb://server:27017?ssl=true', {
201210
sslCA:ca,
202211
sslKey:key,
203212
sslCert:cert,
204-
}, function(err, client) {
213+
});
214+
215+
client.connect(function(err) {
205216
client.close();
206217
});
207218
```

‎docs/reference/content/tutorials/gridfs/streaming.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ In order to use the streaming GridFS API, you first need to create
3030
a `GridFSBucket`.
3131

3232
```javascript
33-
mongodb.MongoClient.connect(uri, function(error, client) {
33+
const client = new mongodb.MongoClient(uri);
34+
client.connect(function(error) {
3435
assert.ifError(error);
3536

3637
const db = client.db(dbName);
@@ -54,7 +55,9 @@ const mongodb = require('mongodb');
5455
const uri = 'mongodb://localhost:27017';
5556
const dbName = 'test';
5657

57-
mongodb.MongoClient.connect(uri, function(error, client) {
58+
const client = new mongodb.MongoClient(uri);
59+
60+
client.connect(unction(error) {
5861
assert.ifError(error);
5962

6063
const db = client.db(dbName);

‎docs/reference/layouts/shortcodes/basic-connection.html

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
// Database Name
88
const dbName = 'myproject';
99

10-
// Use connect method to connect to the server
11-
MongoClient.connect(url, function(err, client) {
10+
// Create a new MongoClient
11+
const client = new MongoClient(url);
12+
13+
// Use connect method to connect to the Server
14+
client.connect(function(err) {
1215
assert.equal(null, err);
1316
console.log("Connected successfully to server");
1417

‎docs/reference/layouts/shortcodes/connect-to-replicaset.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
// Database Name
88
const dbName = 'myproject';
99

10+
// Create a new MongoClient
11+
const client = new MongoClient(url);
12+
1013
// Use connect method to connect to the Server
11-
MongoClient.connect(url, function(err, client) {
14+
client.connect(function(err) {
1215
assert.equal(null, err);
1316
console.log("Connected correctly to server");
1417

‎docs/reference/layouts/shortcodes/connect-to-sharded-cluster.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
// Database Name
88
const dbName = 'myproject';
99

10+
// Create a new MongoClient
11+
const client = new MongoClient(url);
12+
1013
// Use connect method to connect to the Server
11-
MongoClient.connect(url, function(err, client) {
14+
client.connect(function(err, client) {
1215
assert.equal(null, err);
1316
console.log("Connected successfully to server");
1417

‎docs/reference/layouts/shortcodes/find-documents.html

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
// Database Name
88
const dbName = 'myproject';
99

10+
const client = new MongoClient(url);
11+
1012
// Use connect method to connect to the Server
11-
MongoClient.connect(url, function(err, client) {
13+
client.connect(function(err, client) {
1214
assert.equal(null, err);
1315
console.log("Connected correctly to server");
1416

‎docs/reference/layouts/shortcodes/js6-connect.html

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
// Database name
77
const dbName = 'myproject';
88

9+
const client = new MongoClient(url);
10+
911
(async function() {
1012
try {
11-
const client = await MongoClient.connect(url);
13+
await client.connect(url);
1214
console.log("Connected correctly to server");
1315
const db = client.db(dbName);

‎docs/reference/layouts/shortcodes/myproject-connect.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
// Database Name
88
const dbName = 'myproject';
99

10+
// Create a new MongoClient
11+
const client = new MongoClient(url);
12+
1013
// Use connect method to connect to the Server
11-
MongoClient.connect(url, function(err, client) {
14+
client.connect(url, function(err, client) {
1215
assert.equal(null, err);
1316
console.log("Connected correctly to server");
1417

0 commit comments

Comments
 (0)
Please sign in to comment.