-
-
Save ar5had/b402fcada5117b8a2f17859a753bf9b9 to your computer and use it in GitHub Desktop.
| // 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. | |
| }); | |
| ... |
The above way of defining options is now deprecated. It will show
the server/replset/mongos/db options are deprecated, all their options are supported at the top level of the options object [poolSize,ssl,sslValidate,sslCA,sslCert,sslKey,sslPass,sslCRL,autoReconnect,noDelay,keepAlive,keepAliveInitialDelay,connectTimeoutMS,family,socketTimeoutMS,reconnectTries,reconnectInterval,ha,haInterval,replicaSet,secondaryAcceptableLatencyMS,acceptableLatencyMS,connectWithNoPrimary,authSource,w,wtimeout,j,forceServerObjectId,serializeFunctions,ignoreUndefined,raw,bufferMaxEntries,readPreference,pkFactory,promiseLibrary,readConcern,maxStalenessSeconds,loggerLevel,logger,promoteValues,promoteBuffers,promoteLongs,domainsEnabled,checkServerIdentity,validateOptions,appname,auth,user,password,authMechanism,compression,fsync,readPreferenceTags,numberOfRetries,auto_reconnect,minSize,monitorCommands,retryWrites,useNewUrlParser]
I used the options as below and no error was shown
var options = { keepAlive: 300000, connectTimeoutMS: 30000, useNewUrlParser: true};
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')
});
@taourate tx code is fine and working
you saved me too :)
i connected my mlab acc ,but i'm not able to access the collection in the database
Thank you. You saved me.