Skip to content

Instantly share code, notes, and snippets.

@ppbntl19
Last active November 11, 2016 06:53
Show Gist options
  • Save ppbntl19/4c7b64cfb326f721ace5180a2fa3b876 to your computer and use it in GitHub Desktop.
Save ppbntl19/4c7b64cfb326f721ace5180a2fa3b876 to your computer and use it in GitHub Desktop.
mLab recommended mongoose connection options. More supported connections for the underlying Node Native driver can be found here: http://mongodb.github.io/node-mongodb-native/
// mongoose 4.3.x
var mongoose = require('mongoose');
/*
* Mongoose by default sets the auto_reconnect option to true.
* We recommend setting socket options at both the server and replica set level.
* We recommend a 30 second connection timeout because it allows for
* plenty of time in most operating environments.
*/
var options = {
db: {
safe: true
},
server: {
socketOptions: {
keepAlive: 1,
connectTimeoutMS: 30000,
autoReconnect: true
}
},
replset: {
socketOptions: {
keepAlive: 1,
connectTimeoutMS : 30000,
autoReconnect: true
}
}
};
var mongodbUri = 'mongodb://user:pass@host:port/db';
mongoose.connect(mongodbUri, options);
var conn = mongoose.connection;
conn.on('error', console.error.bind(console, 'connection error:'));
conn.once('open', function() {
// Wait for the database connection to establish, then start the app.
});
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment