Mongoose.prototype.connect()

Parameters
  • uri(s) «String»
  • [options] «Object» passed down to the MongoDB driver’s connect() function, except for 4 mongoose-specific options explained below.

  • [options.bufferCommands=true] «Boolean» Mongoose specific option. Set to false to disable buffering on all models associated with this connection.

  • [options.bufferTimeoutMS=true] «Number» Mongoose specific option. If bufferCommands is true, Mongoose will throw an error after bufferTimeoutMS if the operation is still buffered.

  • [options.dbName] «String» The name of the database we want to use. If not provided, use database name from connection string.

  • [options.user] «String» username for authentication, equivalent to options.auth.user. Maintained for backwards compatibility.

  • [options.pass] «String» password for authentication, equivalent to options.auth.password. Maintained for backwards compatibility.

  • [options.poolSize=5] «Number» 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.

  • [options.useUnifiedTopology=false] «Boolean» False by default. Set to true to opt in to the MongoDB driver’s replica set and sharded cluster monitoring engine.

  • [options.serverSelectionTimeoutMS] «Number» 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).

  • [options.heartbeatFrequencyMS] «Number» 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.

  • [options.autoIndex=true] «Boolean» Mongoose-specific option. Set to false to disable automatic index creation for all models associated with this connection.

  • [options.useNewUrlParser=false] «Boolean» False by default. Set to true to opt in to the MongoDB driver’s new URL parser logic.

  • [options.useCreateIndex=true] «Boolean» Mongoose-specific option. If true, this connection will use createIndex() instead of ensureIndex() for automatic index builds via Model.init().

  • [options.useFindAndModify=true] «Boolean» True by default. Set to false to make findOneAndUpdate() and findOneAndRemove() use native findOneAndUpdate() rather than findAndModify().

  • [options.reconnectTries=30] «Number» 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.

  • [options.reconnectInterval=1000] «Number» See reconnectTries option above.

  • [options.promiseLibrary] «Class» Sets the underlying driver’s promise library.

  • [options.bufferMaxEntries] «Number» This option does nothing if useUnifiedTopology is set. 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.

  • [options.connectTimeoutMS=30000] «Number» 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.

  • [options.socketTimeoutMS=30000] «Number» 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 after the MongoDB driver successfully completes.

  • [options.family=0] «Number» Passed transparently to Node.js’ dns.lookup() function. May be either 0, 4, or 6. 4 means use IPv4 only, 6 means use IPv6 only, 0 means try both.

  • [options.autoCreate=false] «Boolean» Set to true to make Mongoose automatically call createCollection() on every model created on this connection.

  • [callback] «Function»

Returns:
  • «Promise» resolves to this if connection succeeded

Opens the default mongoose connection.

Example:

  1. mongoose.connect('mongodb://user:pass@localhost:port/database');
  2. // replica sets
  3. const uri = 'mongodb://user:pass@localhost:port,anotherhost:port,yetanother:port/mydatabase';
  4. mongoose.connect(uri);
  5. // with options
  6. mongoose.connect(uri, options);
  7. // optional callback that gets fired when initial connection completed
  8. const uri = 'mongodb://nonexistent.domain:27000';
  9. mongoose.connect(uri, function(error) {
  10. // if error is truthy, the initial connection failed.
  11. })