Skip to content

Commit cb9d915

Browse files
authored
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

0 commit comments

Comments
 (0)