Skip to content

Commit 09dae52

Browse files
committedSep 1, 2021
docs: remove useNewUrlParser, useUnifiedTopology, some other legacy options from docs
Fix #10631 Fix #10632
1 parent d278258 commit 09dae52

File tree

6 files changed

+15
-108
lines changed

6 files changed

+15
-108
lines changed
 

‎docs/connections.md

+12-86
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
You can connect to MongoDB with the `mongoose.connect()` method.
44

55
```javascript
6-
mongoose.connect('mongodb://localhost:27017/myapp', {useNewUrlParser: true});
6+
mongoose.connect('mongodb://localhost:27017/myapp');
77
```
88

99
This is the minimum needed to connect the `myapp` database running locally
@@ -13,7 +13,7 @@ on the default port (27017). If connecting fails on your machine, try using
1313
You can also specify several more parameters in the `uri`:
1414

1515
```javascript
16-
mongoose.connect('mongodb://username:password@host:port/database?options...', {useNewUrlParser: true});
16+
mongoose.connect('mongodb://username:password@host:port/database?options...');
1717
```
1818

1919
See the [mongodb connection string spec](http://docs.mongodb.org/manual/reference/connection-string/) for more detail.
@@ -40,7 +40,7 @@ Mongoose lets you start using your models immediately, without waiting for
4040
mongoose to establish a connection to MongoDB.
4141

4242
```javascript
43-
mongoose.connect('mongodb://localhost:27017/myapp', {useNewUrlParser: true});
43+
mongoose.connect('mongodb://localhost:27017/myapp');
4444
const MyModel = mongoose.model('Test', new Schema({ name: String }));
4545
// Works
4646
MyModel.findOne(function(error, result) { /* ... */ });
@@ -57,7 +57,7 @@ const MyModel = mongoose.model('Test', new Schema({ name: String }));
5757
MyModel.findOne(function(error, result) { /* ... */ });
5858

5959
setTimeout(function() {
60-
mongoose.connect('mongodb://localhost:27017/myapp', {useNewUrlParser: true});
60+
mongoose.connect('mongodb://localhost:27017/myapp');
6161
}, 60000);
6262
```
6363

@@ -102,12 +102,12 @@ There are two classes of errors that can occur with a Mongoose connection.
102102
To handle initial connection errors, you should use `.catch()` or `try/catch` with async/await.
103103

104104
```javascript
105-
mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true }).
105+
mongoose.connect('mongodb://localhost:27017/test').
106106
catch(error => handleError(error));
107107

108108
// Or:
109109
try {
110-
await mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true });
110+
await mongoose.connect('mongodb://localhost:27017/test');
111111
} catch (error) {
112112
handleError(error);
113113
}
@@ -146,32 +146,16 @@ exceptions that are explained below.
146146

147147
Below are some of the options that are important for tuning Mongoose.
148148

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.
152149
* `promiseLibrary` - Sets the [underlying driver's promise library](http://mongodb.github.io/node-mongodb-native/3.1/api/MongoClient.html).
153150
* `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).
154151
* `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.
155152
* `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 })`
156153
* `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
159-
running **without** [the `useUnifiedTopology` option](/docs/deprecations.html#useunifiedtopology):
160-
161-
* `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
168-
running **with** [the `useUnifiedTopology` option](/docs/deprecations.html#useunifiedtopology):
169-
170-
* `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.
172156

173157
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()`
175159
will retry for 30 seconds by default (default `serverSelectionTimeoutMS`) before
176160
erroring out. To get faster feedback on failed operations, you can reduce `serverSelectionTimeoutMS`
177161
to 5000 as shown below.
@@ -262,8 +246,7 @@ connection may emit.
262246

263247
When you're connecting to a single MongoDB server (a "standalone"), Mongoose will emit 'disconnected' if it gets
264248
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.
267250

268251
<h3 id="keepAlive"><a href="#keepAlive">A note about keepAlive</a></h3>
269252

@@ -304,10 +287,8 @@ mongoose.connect('mongodb://host1:port1/?replicaSet=rsName');
304287

305288
<h3 id="server-selection"><a href="#server-selection">Server Selection</a></h3>
306289

307-
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`,
311292
you'll get the below error:
312293

313294
```
@@ -319,8 +300,6 @@ to `mongoose.connect()`:
319300

320301
```javascript
321302
mongoose.connect(uri, {
322-
useNewUrlParser: true,
323-
useUnifiedTopology: true,
324303
serverSelectionTimeoutMS: 5000 // Timeout after 5s instead of 30s
325304
});
326305
```
@@ -337,8 +316,6 @@ const uri = 'mongodb+srv://username:badpw@cluster0-OMITTED.mongodb.net/' +
337316
'test?retryWrites=true&w=majority';
338317
// Prints "MongoServerError: bad auth Authentication failed."
339318
mongoose.connect(uri, {
340-
useNewUrlParser: true,
341-
useUnifiedTopology: true,
342319
serverSelectionTimeoutMS: 5000
343320
}).catch(err => console.log(err.reason));
344321
```
@@ -468,57 +445,6 @@ const uri = 'mongodb://localhost:27017/test?poolSize=4';
468445
mongoose.createConnection(uri);
469446
```
470447

471-
<h3 id="v5-changes"><a href="#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-
522448
<h3 id="next">Next Up</h3>
523449

524450
Now that we've covered connections, let's take a look at [models](/docs/models.html).

‎docs/lambda.md

-12
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ exports.handler = async function(event, context) {
2323
// potentially expensive process of connecting to MongoDB every time.
2424
if (conn == null) {
2525
conn = mongoose.createConnection(uri, {
26-
useNewUrlParser: true,
27-
useUnifiedTopology: true,
28-
// Buffering means mongoose will queue up operations if it gets
29-
// disconnected from MongoDB and send them when it reconnects.
30-
// With serverless, better to fail fast if not connected.
31-
bufferCommands: false, // Disable mongoose buffering
3226
// and tell the MongoDB driver to not wait more than 5 seconds
3327
// before erroring out if it isn't connected
3428
serverSelectionTimeoutMS: 5000
@@ -66,9 +60,6 @@ const uri = 'YOUR CONNECTION STRING HERE';
6660
exports.connect = async function() {
6761
if (conn == null) {
6862
conn = mongoose.createConnection(uri, {
69-
useNewUrlParser: true,
70-
useUnifiedTopology: true,
71-
bufferCommands: false, // Disable mongoose buffering
7263
serverSelectionTimeoutMS: 5000
7364
});
7465

@@ -97,9 +88,6 @@ const uri = 'YOUR CONNECTION STRING HERE';
9788
exports.connect = async function() {
9889
if (conn == null) {
9990
conn = mongoose.connect(uri, {
100-
useNewUrlParser: true,
101-
useUnifiedTopology: true,
102-
bufferCommands: false, // Disable mongoose buffering
10391
serverSelectionTimeoutMS: 5000
10492
}).then(() => mongoose);
10593

‎docs/models.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ uses is open. Every model has an associated connection. When you use
6363
`mongoose.model()`, your model will use the default mongoose connection.
6464

6565
```javascript
66-
mongoose.connect('mongodb://localhost/gettingstarted', {useNewUrlParser: true});
66+
mongoose.connect('mongodb://localhost/gettingstarted');
6767
```
6868

6969
If you create a custom connection, use that connection's `model()` function

‎docs/transactions.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ or [`Connection#startSession()`](/docs/api/connection.html#connection_Connection
2020
const session = await mongoose.startSession();
2121

2222
// Using custom connection
23-
const db = await mongoose.createConnection(mongodbUri, { useUnifiedTopology: true, useNewUrlParser: true });
23+
const db = await mongoose.createConnection(mongodbUri);
2424
const session = await db.startSession();
2525
```
2626

‎docs/typescript/query-helpers.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ const ProjectModel = model<Project, Model<Project, ProjectQueryHelpers>>('Projec
5050
run().catch(err => console.log(err));
5151

5252
async function run(): Promise<void> {
53-
await connect('mongodb://localhost:27017/test', {
54-
useNewUrlParser: true,
55-
useUnifiedTopology: true
56-
});
53+
await connect('mongodb://localhost:27017/test');
5754

5855
// Equivalent to `ProjectModel.find({ stars: { $gt: 1000 }, name: 'mongoose' })`
5956
await ProjectModel.find().where('stars').gt(1000).byName('mongoose');

‎lib/connection.js

-4
Original file line numberDiff line numberDiff line change
@@ -659,10 +659,6 @@ Connection.prototype.onOpen = function() {
659659
* @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).
660660
* @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.
661661
* @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.
666662
* @param {Class} [options.promiseLibrary] Sets the [underlying driver's promise library](http://mongodb.github.io/node-mongodb-native/3.1/api/MongoClient.html).
667663
* @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).
668664
* @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

Comments
 (0)
Please sign in to comment.