Skip to content

Commit

Permalink
docs(connect): remove references to MongoClient.connect
Browse files Browse the repository at this point in the history
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
  • Loading branch information
daprahamian authored Aug 2, 2018
1 parent b8d2f1d commit cb9d915
Show file tree
Hide file tree
Showing 16 changed files with 174 additions and 122 deletions.
21 changes: 13 additions & 8 deletions docs/reference/content/quick-start/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ const url = 'mongodb://localhost:27017';

// Database Name
const dbName = 'myproject';
const client = new MongoClient(url);

// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
client.connect(url, function(err) {
assert.equal(null, err);
console.log("Connected successfully to server");

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


This query returns all the documents in the **documents** collection. Add the **findDocument** method to the **MongoClient.connect** callback:
This query returns all the documents in the **documents** collection. Add the **findDocument** method to the **client.connect** callback:

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

const client = new MongoClient(url);
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
client.connect(function(err) {
assert.equal(null, err);
console.log("Connected correctly to server");

Expand Down Expand Up @@ -229,7 +231,7 @@ const updateDocument = function(db, callback) {
}
```

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.
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.

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

const client = new MongoClient(url);
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
client.connect(function(err) {
assert.equal(null, err);
console.log("Connected successfully to server");

Expand Down Expand Up @@ -274,7 +277,7 @@ const removeDocument = function(db, callback) {
}
```

Add the new method to the **MongoClient.connect** callback function.
Add the new method to the **client.connect** callback function.

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

const client = new MongoClient(url);
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
client.connect(function(err) {
assert.equal(null, err);
console.log("Connected successfully to server");

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

const dbName = 'myproject';

const client = new MongoClient(url);
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
client.connect(function(err) {
assert.equal(null, err);
console.log("Connected successfully to server");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ title = "Connection Settings"

# URI Connection Settings

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.
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.

```js
const MongoClient = require('mongodb').MongoClient;
Expand All @@ -20,11 +20,14 @@ const assert = require('assert');
const url = 'mongodb://localhost:50000,localhost:50001';
// Database Name
const dbName = 'myproject';
// Use connect method to connect to the Server passing in
// additional options
MongoClient.connect(url, {

// create a client, passing in additional options
const client = new MongoClient(url, {
poolSize: 10, ssl: true
}, function(err, client) {
});

// Use connect method to connect to the server
client.connect(function(err) {
assert.equal(null, err);
console.log("Connected correctly to server");

Expand Down
10 changes: 4 additions & 6 deletions docs/reference/content/reference/ecmascriptnext/connecting.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,19 @@ const assert = require('assert');
const url = 'mongodb://localhost:27017/myproject';
// Database Name
const dbName = 'myproject';
let client;
const client = new MongoClient(url);

try {
// Use connect method to connect to the Server
client = await MongoClient.connect(url);
await client.connect();

const db = client.db(dbName);
} catch (err) {
console.log(err.stack);
}

if (client) {
client.close();
}
client.close();
})();
```

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.
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.
44 changes: 22 additions & 22 deletions docs/reference/content/reference/ecmascriptnext/crud.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ const url = 'mongodb://localhost:27017';
const dbName = 'myproject';

(async function() {
let client;
const client = new MongoClient(url);

try {
client = await MongoClient.connect(url);
await client.connect();
console.log("Connected correctly to server");

const db = client.db(dbName);
Expand Down Expand Up @@ -61,10 +61,10 @@ const url = 'mongodb://localhost:27017';
const dbName = 'myproject';

(async function() {
let client;
const client = new MongoClient(url);

try {
client = await MongoClient.connect(url);
await client.connect();
console.log("Connected correctly to server");

const db = client.db(dbName);
Expand Down Expand Up @@ -104,10 +104,10 @@ const url = 'mongodb://localhost:27017';
const dbName = 'myproject';

(async function() {
let client;
const client = new MongoClient(url);

try {
client = await MongoClient.connect(url);
await client.connect();
console.log("Connected correctly to server");

const db = client.db(dbName);
Expand Down Expand Up @@ -152,10 +152,10 @@ const url = 'mongodb://localhost:27017';
const dbName = 'myproject';

(async function() {
let client;
const client = new MongoClient(url);

try {
client = await MongoClient.connect(url);
await client.connect();
console.log("Connected correctly to server");

const db = client.db(dbName);
Expand Down Expand Up @@ -195,10 +195,10 @@ const url = 'mongodb://localhost:27017';
const dbName = 'myproject';

(async function() {
let client;
const client = new MongoClient(url);

try {
client = await MongoClient.connect(url);
await client.connect();
console.log("Connected correctly to server");

const db = client.db(dbName);
Expand Down Expand Up @@ -238,10 +238,10 @@ const url = 'mongodb://localhost:27017';
const dbName = 'myproject';

(async function() {
let client;
const client = new MongoClient(url);

try {
client = await MongoClient.connect(url);
await client.connect();
console.log("Connected correctly to server");

const db = client.db(dbName);
Expand Down Expand Up @@ -277,10 +277,10 @@ const url = 'mongodb://localhost:27017';
const dbName = 'myproject';

(async function() {
let client;
const client = new MongoClient(url);

try {
client = await MongoClient.connect(url);
await client.connect();
console.log("Connected correctly to server");

const db = client.db(dbName);
Expand Down Expand Up @@ -327,10 +327,10 @@ const url = 'mongodb://localhost:27017';
const dbName = 'myproject';

(async function() {
let client;
const client = new MongoClient(url);

try {
client = await MongoClient.connect(url);
await client.connect();
console.log("Connected correctly to server");

const db = client.db(dbName);
Expand Down Expand Up @@ -417,10 +417,10 @@ const url = 'mongodb://localhost:27017';
const dbName = 'myproject';

(async function() {
let client;
const client = new MongoClient(url);

try {
client = await MongoClient.connect(url);
await client.connect();
console.log("Connected correctly to server");

const db = client.db(dbName);
Expand Down Expand Up @@ -454,10 +454,10 @@ const url = 'mongodb://localhost:27017';
const dbName = 'myproject';

(async function() {
let client;
const client = new MongoClient(url);

try {
client = await MongoClient.connect(url);
await client.connect();
console.log("Connected correctly to server");

const db = client.db(dbName);
Expand Down Expand Up @@ -497,10 +497,10 @@ const url = 'mongodb://localhost:27017';
const dbName = 'myproject';

(async function() {
let client;
const client = new MongoClient(url);

try {
client = await MongoClient.connect(url);
await client.connect();
console.log("Connected correctly to server");

const db = client.db(dbName);
Expand Down
9 changes: 5 additions & 4 deletions docs/reference/content/reference/faq/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ title = "Frequently Asked Questions"

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

| Setting | Default Value MongoClient.connect | Description |
| Setting | Default Value client.connect | Description |
| :----------| :------------- | :------------- |
| 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.|
| 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.|
Expand Down Expand Up @@ -106,12 +106,13 @@ are some things to check:
allowing the driver to detect that the socket is closed.
2. The firewall should allow keepAlive probes.

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

```js
MongoClient.connect('mongodb://localhost:27017/test?maxPoolSize=5000',
function(err, client) {
const client = new MongoClient('mongodb://localhost:27017/test?maxPoolSize=5000');
client.connect('mongodb://localhost:27017/test?maxPoolSize=5000',
function(err) {
// connection
});
```
Expand Down
15 changes: 9 additions & 6 deletions docs/reference/content/reference/management/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myprojeect';

// Use connect method to connect to the Server
MongoClient.connect(url, function(err, client) {
const client = new MongoClient(url);
// Use connect method to connect to the server
client.connect(function(err) {
assert.equal(null, err);
console.log("Connected correctly to server");

Expand Down Expand Up @@ -61,8 +62,9 @@ const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myprojeect';

// Use connect method to connect to the Server
MongoClient.connect(url, function(err, client) {
const client = new MongoClient(url);
// Use connect method to connect to the server
client.connect(function(err) {
assert.equal(null, err);
console.log("Connected correctly to server");

Expand Down Expand Up @@ -129,8 +131,9 @@ const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myprojeect';

// Use connect method to connect to the Server
MongoClient.connect(url, function(err, client) {
const client = new MongoClient(url);
// Use connect method to connect to the server
client.connect(function(err) {
assert.equal(null, err);
console.log("Connected correctly to server");

Expand Down
Loading

0 comments on commit cb9d915

Please sign in to comment.