How to fix 'the server/replset/mongos options are deprecated, all their options are supported at the top level of the options object' in NodeJs


I am using following Node code to connect to mongoDB

var mongodbUri = "mongodb://localhost:27017/db_name"
var settings = {
      server : {
        reconnectTries : Number.MAX_VALUE,
        autoReconnect : true
      }
    };
MongoClient.connect(mongodbUri, settings, function(err, dbref) {
  if (!err) {
    console.log("Mongodb connected");
    db = dbref;
  }else{
    console.log("Error while connecting to mongoDB" + err);
  }
});

With this code i am getting the following warning message on startup

the server/replset/mongos 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,connectTimeoutMS,
socketTimeoutMS,reconnectTries,reconnectInterval,ha,haInterval,replicaSet,secondaryAcceptableLatencyMS,
acceptableLatencyMS,connectWithNoPrimary,authSource,w,wtimeout,j,forceServerObjectId,serializeFunctions
,ignoreUndefined,raw,promoteLongs,bufferMaxEntries,readPreference,pkFactory,promiseLibrary,readConcern,
maxStalenessSeconds,loggerLevel,logger,promoteValues,promoteBuffers,promoteLongs,domainsEnabled,
keepAliveInitialDelay,checkServerIdentity,validateOptions]

1 Answer

7 years ago by

As warning message saying server/replset/mongos options are deprecated, So you should not use them anymore instead you just need to move all of your server/replset/mongos options to root.
Update your code to following

var mongodbUri = "mongodb://localhost:27017/db_name"
var settings = {
      reconnectTries : Number.MAX_VALUE,
      autoReconnect : true
};
MongoClient.connect(mongodbUri, settings, function(err, dbref) {
  if (!err) {
    console.log("Mongodb connected");
    db = dbref;
  }else{
    console.log("Error while connecting to mongoDB" + err);
  }
});
7 years ago by Karthik Divi