Skip to content

Instantly share code, notes, and snippets.

@ar5had
Created January 20, 2017 07:17
Show Gist options
  • Save ar5had/b402fcada5117b8a2f17859a753bf9b9 to your computer and use it in GitHub Desktop.
Save ar5had/b402fcada5117b8a2f17859a753bf9b9 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 = { server: { socketOptions: { keepAlive: 300000, connectTimeoutMS: 30000 } },
replset: { socketOptions: { keepAlive: 300000, connectTimeoutMS : 30000 } } };
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.
});
...
@taourarte
Copy link

taourarte commented Sep 9, 2018

connecting your app with mlab database

const mongoose = require('mongoose');
var mongodbUri ='mongodb://@ds249992.mlab.com:49992/databasename';
mongoose.connect(mongodbUri, {
  useNewUrlParser: true,
  auth: {
    user: 'UserName',
    password: 'Password'
  }
})
var conn = mongoose.connection;    
conn.on('error', console.error.bind(console, 'connection error:'));  
 
conn.once('open', () =>{
 console.log('connected to adatabase')                       
});

@Devcorns
Copy link

@taourate tx code is fine and working

@shsunmoonlee
Copy link

you saved me too :)

@kraravind167
Copy link

i connected my mlab acc ,but i'm not able to access the collection in the database

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment