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 afterbufferTimeoutMS
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 forserverSelectionTimeoutMS
milliseconds before erroring out. If not set, the MongoDB driver defaults to using30000
(30 seconds).[options.heartbeatFrequencyMS] «Number» If
useUnifiedTopology = true
, the MongoDB driver sends a heartbeat everyheartbeatFrequencyMS
to check on the status of the connection. A heartbeat is subject toserverSelectionTimeoutMS
, 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 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.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.[options.autoCreate=false] «Boolean» Set to
true
to make Mongoose automatically callcreateCollection()
on every model created on this connection.[callback] «Function»
Returns:
- «Promise» resolves to
this
if connection succeeded
Opens the default mongoose connection.
Example:
mongoose.connect('mongodb://user:pass@localhost:port/database');
// replica sets
const uri = 'mongodb://user:pass@localhost:port,anotherhost:port,yetanother:port/mydatabase';
mongoose.connect(uri);
// with options
mongoose.connect(uri, options);
// optional callback that gets fired when initial connection completed
const uri = 'mongodb://nonexistent.domain:27000';
mongoose.connect(uri, function(error) {
// if error is truthy, the initial connection failed.
})