Skip to content

Commit

Permalink
docs(connections): add note about the family option for IPv4 vs IPv…
Browse files Browse the repository at this point in the history
…6 and add port to example URIs

Fix #6566
  • Loading branch information
vkarpov15 committed Jul 23, 2018
1 parent 95e553f commit 559ae4e
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions docs/connections.jade
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ block content
You can connect to MongoDB with the `mongoose.connect()` method.

```javascript
mongoose.connect('mongodb://localhost/myapp');
mongoose.connect('mongodb://localhost:27017/myapp');
```

This is the minimum needed to connect the `myapp` database running locally
on the default port (27017). If the local connection fails then try using
127.0.0.1 instead of localhost. Sometimes issues may arise when the local
hostname has been changed.
on the default port (27017). If connecting fails on your machine, try using
`127.0.0.1` instead of `localhost`.

You can also specify several more parameters in the `uri`:

Expand All @@ -29,7 +28,7 @@ block content
mongoose to establish a connection to MongoDB.

```javascript
mongoose.connect('mongodb://localhost/myapp');
mongoose.connect('mongodb://localhost:27017/myapp');
var MyModel = mongoose.model('Test', new Schema({ name: String }));
// Works
MyModel.findOne(function(error, result) { /* ... */ });
Expand All @@ -46,7 +45,7 @@ block content
MyModel.findOne(function(error, result) { /* ... */ });

setTimeout(function() {
mongoose.connect('mongodb://localhost/myapp');
mongoose.connect('mongodb://localhost:27017/myapp');
}, 60000);
```

Expand Down Expand Up @@ -87,6 +86,7 @@ block content
* `bufferMaxEntries` - The MongoDB driver also has its own buffering mechanism that kicks in when the driver is disconnected. Set this option to 0 and set `bufferCommands` to `false` on your schemas if you want your database operations to fail immediately when the driver is not connected, as opposed to waiting for reconnection.
* `connectTimeoutMS` - How long the MongoDB driver will wait before failing its initial connection attempt. Once Mongoose has successfully connected, `connectTimeoutMS` is no longer relevant.
* `socketTimeoutMS` - How long the MongoDB driver will wait before killing an inactive socket. A socket may be inactive because of either no activity or a long-running operation. This is set to `30000` by default, you should set this to 2-3x your longest running operation if you expect some of your database operations to run longer than 20 seconds.
* `family` - Whether to connect using IPv4 or IPv6. This option passed to [Node.js' `dns.lookup()`](https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_options_callback) function. If you don't specify this option, the MongoDB driver will try IPv6 first and then IPv4 if IPv6 fails. If your `mongoose.connect(uri)` call takes a long time, try `mongoose.connect(uri, { family: 4 })`

Example:

Expand All @@ -101,6 +101,7 @@ block content
bufferMaxEntries: 0,
connectTimeoutMS: 10000, // Give up initial connection after 10 seconds
socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
family: 4 // Use IPv4, skip trying IPv6
};
mongoose.connect(uri, options);
```
Expand Down Expand Up @@ -164,8 +165,8 @@ block content
```javascript
mongoose.connect(uri, { keepAlive: true, keepAliveInitialDelay: 300000 });
```
`keepAliveInitialDelay` is the number of milliseconds to wait before initiating `keepAlive` on the socket.

`keepAliveInitialDelay` is the number of milliseconds to wait before initiating `keepAlive` on the socket.
`keepAlive` is true by default since mongoose 5.2.0.

<h3 id="replicaset_connections"><a href="#replicaset_connections">Replica Set Connections</a></h3>
Expand Down Expand Up @@ -231,7 +232,7 @@ block content
// With object options
mongoose.createConnection(uri, { poolSize: 4 });

const uri = 'mongodb://localhost/test?poolSize=4';
const uri = 'mongodb://localhost:27017/test?poolSize=4';
mongoose.createConnection(uri);
```

Expand Down

2 comments on commit 559ae4e

@AbdelrahmanHafez
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The performance effect for developers who run Windows is hidden in between the lines, perhaps add a standalone note for this and link to the issue for more info?

@vkarpov15
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will add it to the FAQ, see #6784.

Please sign in to comment.