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
Copy file name to clipboardExpand all lines: docs/async-await.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -82,7 +82,7 @@ async function doStuffWithUser() {
82
82
}
83
83
```
84
84
85
-
<h2id="queries">Async/Await with Mongoose Queries</h2>
85
+
## Async/Await with Mongoose Queries {#queries}
86
86
87
87
Under the hood, [async/await is syntactic sugar](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await) over the Promise API.
88
88
Due to the surprisingly simple way promises are implemented in JavaScript, the keyword `await` will try to unwrap any object with a property whose key is the string ‘then’ and whose value is a function.
The `connect` method also accepts an `options` object which will be passed
135
135
on to the underlying MongoDB driver.
@@ -158,7 +158,7 @@ Below are some of the options that are important for tuning Mongoose.
158
158
*`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).
159
159
*`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.
The `serverSelectionTimeoutMS` option is extremely important: it controls how long the MongoDB Node.js driver will attempt to retry any operation before erroring out.
164
164
This includes initial connection, like `await mongoose.connect()`, as well as any operations that make requests to MongoDB, like `save()` or `find()`.
You can also specify driver options in your connection string as
231
231
[parameters in the query string](https://en.wikipedia.org/wiki/Query_string)
@@ -258,7 +258,7 @@ are closely associated with the hostname and authentication information.
258
258
*`authSource` - The database to use when authenticating with `user` and `pass`. In MongoDB, [users are scoped to a database](https://www.mongodb.com/docs/manual/tutorial/manage-users-and-roles/). If you are getting an unexpected login failure, you may need to set this option.
259
259
*`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 })`
<h2id="keepAlive"><ahref="#keepAlive">A note about keepAlive</a></h2>
307
+
## A note about keepAlive {#keepAlive}
308
308
309
309
Before Mongoose 5.2.0, you needed to enable the `keepAlive` option to initiate [TCP keepalive](https://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html) to prevent `"connection closed"` errors.
310
310
However, `keepAlive` has been `true` by default since Mongoose 5.2.0, and the `keepAlive` is deprecated as of Mongoose 7.2.0.
311
311
Please remove `keepAlive` and `keepAliveInitialDelay` options from your Mongoose connections.
312
312
313
-
<h2id="replicaset_connections"><ahref="#replicaset_connections">Replica Set Connections</a></h2>
313
+
## Replica Set Connections {#replicaset_connections}
314
314
315
315
To connect to a replica set you pass a comma delimited list of hosts to
316
316
connect to rather than a single host.
@@ -331,7 +331,7 @@ To connect to a single node replica set, specify the `replicaSet` option.
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.
337
337
If the MongoDB driver can't find a server to send an operation to after `serverSelectionTimeoutMS`,
@@ -366,7 +366,7 @@ mongoose.connect(uri, {
366
366
}).catch(err=>console.log(err.reason));
367
367
```
368
368
369
-
<h2id="replicaset-hostnames"><ahref="#replicaset-hostnames">Replica Set Host Names</a></h2>
369
+
## Replica Set Host Names {#replicaset-hostnames}
370
370
371
371
MongoDB replica sets rely on being able to reliably figure out the domain name for each member.
372
372
On Linux and OSX, the MongoDB server uses the output of the [`hostname` command](https://linux.die.net/man/1/hostname) to figure out the domain name to report to the replica set.
@@ -399,7 +399,7 @@ if (err.name === 'MongooseServerSelectionError') {
So far we've seen how to connect to MongoDB using Mongoose's default
416
416
connection. Mongoose creates a *default connection* when you call `mongoose.connect()`.
@@ -493,7 +493,7 @@ module.exports = conn;
493
493
You can create separate files for each connection, like `connections/web.js` and `connections/mobile.js` if you want to create separate connections for your web API backend and your mobile API backend.
494
494
Your business logic can then `require()` or `import` the connection it needs.
The connection pool size is important because [MongoDB currently can only process one operation per socket](https://thecodebarbarian.com/slow-trains-in-mongodb-and-nodejs).
513
513
So `maxPoolSize` functions as a cap on the number of concurrent operations.
## Multi Tenant Connections {#multi-tenant-connections}
516
516
517
517
In the context of Mongoose, a multi-tenant architecture typically means a case where multiple different clients talk to MongoDB through a single Mongoose application.
518
518
This typically means each client makes queries and executes updates through a single Mongoose application, but has a distinct MongoDB database within the same MongoDB cluster.
0 commit comments