Mongoose.prototype.createConnection()

Parameters
  • [uri] «String» a mongodb:// URI

  • [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.dbName] «String» The name of the database you want to use. If not provided, Mongoose uses the 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.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 make all connections set the useNewUrlParser option by default.

  • [options.useUnifiedTopology=false] «Boolean» False by default. Set to true to make all connections set the useUnifiedTopology option by default.

  • [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.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.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.

Returns:
  • «Connection» the created Connection object. Connections are thenable, so you can do await mongoose.createConnection()

Creates a Connection instance.

Each connection instance maps to a single database. This method is helpful when managing multiple db connections.

Options passed take precedence over options included in connection strings.

Example:

  1. // with mongodb:// URI
  2. db = mongoose.createConnection('mongodb://user:pass@localhost:port/database');
  3. // and options
  4. const opts = { db: { native_parser: true }}
  5. db = mongoose.createConnection('mongodb://user:pass@localhost:port/database', opts);
  6. // replica sets
  7. db = mongoose.createConnection('mongodb://user:pass@localhost:port,anotherhost:port,yetanother:port/database');
  8. // and options
  9. const opts = { replset: { strategy: 'ping', rs_name: 'testSet' }}
  10. db = mongoose.createConnection('mongodb://user:pass@localhost:port,anotherhost:port,yetanother:port/database', opts);
  11. // and options
  12. const opts = { server: { auto_reconnect: false }, user: 'username', pass: 'mypassword' }
  13. db = mongoose.createConnection('localhost', 'database', port, opts)
  14. // initialize now, connect later
  15. db = mongoose.createConnection();
  16. db.openUri('localhost', 'database', port, [opts]);