You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -146,32 +146,16 @@ exceptions that are explained below.
146
146
147
147
Below are some of the options that are important for tuning Mongoose.
148
148
149
-
*`useNewUrlParser` - The underlying MongoDB driver has deprecated their current [connection string](https://docs.mongodb.com/manual/reference/connection-string/) parser. Because this is a major change, they added the `useNewUrlParser` flag to allow users to fall back to the old parser if they find a bug in the new parser. You should set `useNewUrlParser: true` unless that prevents you from connecting. Note that if you specify `useNewUrlParser: true`, you **must** specify a port in your connection string, like `mongodb://localhost:27017/dbname`. The new url parser does _not_ support connection strings that do not have a port, like `mongodb://localhost/dbname`.
150
-
*`useFindAndModify` - True by default. Set to `false` to make `findOneAndUpdate()` and `findOneAndRemove()` use native `findOneAndUpdate()` rather than `findAndModify()`.
151
-
*`useUnifiedTopology`- False by default. Set to `true` to opt in to using [the MongoDB driver's new connection management engine](/docs/deprecations.html#useunifiedtopology). You should set this option to `true`, except for the unlikely case that it prevents you from maintaining a stable connection.
152
149
*`promiseLibrary` - Sets the [underlying driver's promise library](http://mongodb.github.io/node-mongodb-native/3.1/api/MongoClient.html).
153
150
*`poolSize` - The maximum number of sockets the MongoDB driver will keep open for this connection. By default, `poolSize` is 5. Keep in mind that, as of MongoDB 3.4, MongoDB only allows one operation per socket at a time, so you may want to increase this if you find you have a few slow queries that are blocking faster queries from proceeding. See [Slow Trains in MongoDB and Node.js](http://thecodebarbarian.com/slow-trains-in-mongodb-and-nodejs).
154
151
*`socketTimeoutMS` - How long the MongoDB driver will wait before killing a socket due to inactivity _after initial connection_. 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. This option is passed to [Node.js `socket#setTimeout()` function](https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback) after the MongoDB driver successfully completes.
155
152
*`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 })`
156
153
*`authSource` - The database to use when authenticating with `user` and `pass`. In MongoDB, [users are scoped to a database](https://docs.mongodb.com/manual/tutorial/manage-users-and-roles/). If you are getting an unexpected login failure, you may need to set this option.
157
-
158
-
The following options are important for tuning Mongoose only if you are
*`autoReconnect` - The underlying MongoDB driver will automatically try to reconnect when it loses connection to MongoDB. Unless you are an extremely advanced user that wants to manage their own connection pool, do **not** set this option to `false`.
162
-
*`reconnectTries` - If you're connected to a single server or mongos proxy (as opposed to a replica set), the MongoDB driver will try to reconnect every `reconnectInterval` milliseconds for `reconnectTries` times, and give up afterward. When the driver gives up, the mongoose connection emits a `reconnectFailed` event. This option does nothing for replica set connections.
163
-
*`reconnectInterval` - See `reconnectTries`
164
-
*`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.
165
-
*`connectTimeoutMS` - How long the MongoDB driver will wait before killing a socket due to inactivity _during initial connection_. Defaults to 30000. This option is passed transparently to [Node.js' `socket#setTimeout()` function](https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback).
166
-
167
-
The following options are important for tuning Mongoose only if you are
*`serverSelectionTimeoutMS` - With `useUnifiedTopology`, the MongoDB driver will try to find a server to send any given operation to, and keep retrying for `serverSelectionTimeoutMS` milliseconds. If not set, the MongoDB driver defaults to using `30000` (30 seconds).
171
-
*`heartbeatFrequencyMS` - With `useUnifiedTopology`, the MongoDB driver sends a heartbeat every `heartbeatFrequencyMS` to check on the status of the connection. A heartbeat is subject to `serverSelectionTimeoutMS`, so the MongoDB driver will retry failed heartbeats for up to 30 seconds by default. Mongoose only emits a `'disconnected'` event after a heartbeat has failed, so you may want to decrease this setting to reduce the time between when your server goes down and when Mongoose emits `'disconnected'`. We recommend you do **not** set this setting below 1000, too many heartbeats can lead to performance degradation.
154
+
*`serverSelectionTimeoutMS` - The MongoDB driver will try to find a server to send any given operation to, and keep retrying for `serverSelectionTimeoutMS` milliseconds. If not set, the MongoDB driver defaults to using `30000` (30 seconds).
155
+
*`heartbeatFrequencyMS` - The MongoDB driver sends a heartbeat every `heartbeatFrequencyMS` to check on the status of the connection. A heartbeat is subject to `serverSelectionTimeoutMS`, so the MongoDB driver will retry failed heartbeats for up to 30 seconds by default. Mongoose only emits a `'disconnected'` event after a heartbeat has failed, so you may want to decrease this setting to reduce the time between when your server goes down and when Mongoose emits `'disconnected'`. We recommend you do **not** set this setting below 1000, too many heartbeats can lead to performance degradation.
172
156
173
157
The `serverSelectionTimeoutMS` option also handles how long `mongoose.connect()` will
174
-
retry initial connection before erroring out. With `useUnifiedTopology`, `mongoose.connect()`
158
+
retry initial connection before erroring out. `mongoose.connect()`
175
159
will retry for 30 seconds by default (default `serverSelectionTimeoutMS`) before
176
160
erroring out. To get faster feedback on failed operations, you can reduce `serverSelectionTimeoutMS`
177
161
to 5000 as shown below.
@@ -262,8 +246,7 @@ connection may emit.
262
246
263
247
When you're connecting to a single MongoDB server (a "standalone"), Mongoose will emit 'disconnected' if it gets
264
248
disconnected from the standalone server, and 'connected' if it successfully connects to the standalone. In a
265
-
replica set with `useUnifiedTopology = true`, Mongoose will emit 'disconnected' if it loses connectivity to
266
-
_every_ server in the replica set, and 'connected' if it manages to reconnect to at least one server in the replica set.
249
+
replica set, Mongoose will emit 'disconnected' if it loses connectivity to the replica set primary, and 'connected' if it manages to reconnect to a the replica set primary.
267
250
268
251
<h3id="keepAlive"><ahref="#keepAlive">A note about keepAlive</a></h3>
If you enable the `useUnifiedTopology` option, the underlying MongoDB driver
308
-
will use [server selection](https://github.com/mongodb/specifications/blob/master/source/server-selection/server-selection.rst)
309
-
to connect to MongoDB and send operations to MongoDB. If the MongoDB
310
-
driver can't find a server to send an operation to after `serverSelectionTimeoutMS`,
290
+
The underlying MongoDB driver uses a process known as [server selection](https://github.com/mongodb/specifications/blob/master/source/server-selection/server-selection.rst) to connect to MongoDB and send operations to MongoDB.
291
+
If the MongoDB driver can't find a server to send an operation to after `serverSelectionTimeoutMS`,
311
292
you'll get the below error:
312
293
313
294
```
@@ -319,8 +300,6 @@ to `mongoose.connect()`:
319
300
320
301
```javascript
321
302
mongoose.connect(uri, {
322
-
useNewUrlParser:true,
323
-
useUnifiedTopology:true,
324
303
serverSelectionTimeoutMS:5000// Timeout after 5s instead of 30s
325
304
});
326
305
```
@@ -337,8 +316,6 @@ const uri = 'mongodb+srv://username:badpw@cluster0-OMITTED.mongodb.net/' +
337
316
'test?retryWrites=true&w=majority';
338
317
// Prints "MongoServerError: bad auth Authentication failed."
339
318
mongoose.connect(uri, {
340
-
useNewUrlParser:true,
341
-
useUnifiedTopology:true,
342
319
serverSelectionTimeoutMS:5000
343
320
}).catch(err=>console.log(err.reason));
344
321
```
@@ -468,57 +445,6 @@ const uri = 'mongodb://localhost:27017/test?poolSize=4';
468
445
mongoose.createConnection(uri);
469
446
```
470
447
471
-
<h3id="v5-changes"><ahref="#v5-changes">Option Changes in v5.x</a></h3>
472
-
473
-
You may see the following deprecation warning if upgrading from 4.x to 5.x
474
-
and you didn't use the `useMongoClient` option in 4.x:
475
-
476
-
```
477
-
the server/replset/mongos options are deprecated, all their options are supported at the top level of the options object
478
-
```
479
-
480
-
In older version of the MongoDB driver you had to specify distinct options
481
-
for server connections, replica set connections, and mongos connections:
482
-
483
-
```javascript
484
-
mongoose.connect(myUri, {
485
-
server: {
486
-
socketOptions: {
487
-
socketTimeoutMS:0,
488
-
keepAlive:true
489
-
},
490
-
reconnectTries:30
491
-
},
492
-
replset: {
493
-
socketOptions: {
494
-
socketTimeoutMS:0,
495
-
keepAlive:true
496
-
},
497
-
reconnectTries:30
498
-
},
499
-
mongos: {
500
-
socketOptions: {
501
-
socketTimeoutMS:0,
502
-
keepAlive:true
503
-
},
504
-
reconnectTries:30
505
-
}
506
-
});
507
-
```
508
-
509
-
In mongoose v5.x you can instead declare these options at the top level,
510
-
without all that extra nesting.
511
-
[Here's the list of all supported options](http://mongodb.github.io/node-mongodb-native/2.2/api/MongoClient.html).
512
-
513
-
```javascript
514
-
// Equivalent to the above code
515
-
mongoose.connect(myUri, {
516
-
socketTimeoutMS:0,
517
-
keepAlive:true,
518
-
reconnectTries:30
519
-
});
520
-
```
521
-
522
448
<h3id="next">Next Up</h3>
523
449
524
450
Now that we've covered connections, let's take a look at [models](/docs/models.html).
* @param {Number} [options.serverSelectionTimeoutMS] If `useUnifiedTopology = true`, the MongoDB driver will try to find a server to send any given operation to, and keep retrying for `serverSelectionTimeoutMS` milliseconds before erroring out. If not set, the MongoDB driver defaults to using `30000` (30 seconds).
660
660
* @param {Number} [options.heartbeatFrequencyMS] If `useUnifiedTopology = true`, the MongoDB driver sends a heartbeat every `heartbeatFrequencyMS` to check on the status of the connection. A heartbeat is subject to `serverSelectionTimeoutMS`, so the MongoDB driver will retry failed heartbeats for up to 30 seconds by default. Mongoose only emits a `'disconnected'` event after a heartbeat has failed, so you may want to decrease this setting to reduce the time between when your server goes down and when Mongoose emits `'disconnected'`. We recommend you do **not** set this setting below 1000, too many heartbeats can lead to performance degradation.
661
661
* @param {Boolean} [options.autoIndex=true] Mongoose-specific option. Set to false to disable automatic index creation for all models associated with this connection.
662
-
* @param {Boolean} [options.useNewUrlParser=false] False by default. Set to `true` to opt in to the MongoDB driver's new URL parser logic.
663
-
* @param {Boolean} [options.useFindAndModify=true] True by default. Set to `false` to make `findOneAndUpdate()` and `findOneAndRemove()` use native `findOneAndUpdate()` rather than `findAndModify()`.
664
-
* @param {Number} [options.reconnectTries=30] If you're connected to a single server or mongos proxy (as opposed to a replica set), the MongoDB driver will try to reconnect every `reconnectInterval` milliseconds for `reconnectTries` times, and give up afterward. When the driver gives up, the mongoose connection emits a `reconnectFailed` event. This option does nothing for replica set connections.
665
-
* @param {Number} [options.reconnectInterval=1000] See `reconnectTries` option above.
666
662
* @param {Class} [options.promiseLibrary] Sets the [underlying driver's promise library](http://mongodb.github.io/node-mongodb-native/3.1/api/MongoClient.html).
667
663
* @param {Number} [options.connectTimeoutMS=30000] How long the MongoDB driver will wait before killing a socket due to inactivity _during initial connection_. Defaults to 30000. This option is passed transparently to [Node.js' `socket#setTimeout()` function](https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback).
668
664
* @param {Number} [options.socketTimeoutMS=30000] How long the MongoDB driver will wait before killing a socket due to inactivity _after initial connection_. 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. This option is passed to [Node.js `socket#setTimeout()` function](https://nodejs.org/api/net.html#net_socket_settimeout_timeout_callback) after the MongoDB driver successfully completes.
0 commit comments