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 theuseNewUrlParser
option by default.[options.useUnifiedTopology=false] «Boolean» False by default. Set to
true
to make all connections set theuseUnifiedTopology
option by default.[options.useCreateIndex=true] «Boolean» Mongoose-specific option. If
true
, this connection will usecreateIndex()
instead ofensureIndex()
for automatic index builds viaModel.init()
.[options.useFindAndModify=true] «Boolean» True by default. Set to
false
to makefindOneAndUpdate()
andfindOneAndRemove()
use nativefindOneAndUpdate()
rather thanfindAndModify()
.[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 forreconnectTries
times, and give up afterward. When the driver gives up, the mongoose connection emits areconnectFailed
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 setbufferCommands
tofalse
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.jssocket#setTimeout()
function after the MongoDB driver successfully completes.[options.family=0] «Number» Passed transparently to Node.js’
dns.lookup()
function. May be either0
,4
, or6
.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:
// with mongodb:// URI
db = mongoose.createConnection('mongodb://user:pass@localhost:port/database');
// and options
const opts = { db: { native_parser: true }}
db = mongoose.createConnection('mongodb://user:pass@localhost:port/database', opts);
// replica sets
db = mongoose.createConnection('mongodb://user:pass@localhost:port,anotherhost:port,yetanother:port/database');
// and options
const opts = { replset: { strategy: 'ping', rs_name: 'testSet' }}
db = mongoose.createConnection('mongodb://user:pass@localhost:port,anotherhost:port,yetanother:port/database', opts);
// and options
const opts = { server: { auto_reconnect: false }, user: 'username', pass: 'mypassword' }
db = mongoose.createConnection('localhost', 'database', port, opts)
// initialize now, connect later
db = mongoose.createConnection();
db.openUri('localhost', 'database', port, [opts]);