mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead

Open cosminn777 opened this issue 9 years ago • 158 comments

DeprecationWarning: open() is deprecated in mongoose >= 4.11.0, use openUri() instead, or set the useMongoClient option if using connect() or createConnection()

Mongoose 4.11.0, MongoDB 2.2.29, NodeJS 8.1.2

cosminn777 avatar Jun 25 '17 13:06 cosminn777

+1 , I have no idea where to fix to get rid of that warning

alanpurple avatar Jun 25 '17 16:06 alanpurple

The warning is for a code inside Mongoose actually, when connecting string has a replica set:

Mongoose.prototype.connect = function() {
  var conn = this.connection;
  if ((arguments.length === 2 || arguments.length === 3) &&
      typeof arguments[0] === 'string' &&
      typeof arguments[1] === 'object' &&
      arguments[1].useMongoClient === true) {
    return conn.openUri(arguments[0], arguments[1], arguments[2]);
  }
  if (rgxReplSet.test(arguments[0]) || checkReplicaSetInUri(arguments[0])) {
    return new MongooseThenable(this, conn.openSet.apply(conn, arguments));
  }

  return new MongooseThenable(this, conn.open.apply(conn, arguments));
};

tinovyatkin avatar Jun 25 '17 16:06 tinovyatkin

@tinovyatkin

So then, this is a bug?

alanpurple avatar Jun 25 '17 16:06 alanpurple

looks like a bug, can we workaround by adding useMongoClient: true to options (you may see a deprecation from MongoDB driver then but that doesn't throw)

tinovyatkin avatar Jun 25 '17 16:06 tinovyatkin

Adding useMongoClient: true makes the message disappear, but then my documents simply stop loading. Haven’t debugged it deeper, but I would prefer to hear best practices first before I adapt my code.

hagemann avatar Jun 25 '17 17:06 hagemann

My application also doesn't work with the workaround suggested above. It simply doesn't run MyModel.find method anymore and gives no error and no timeout.

artnicbr avatar Jun 25 '17 17:06 artnicbr

The reason why "useMongoclient: true" option doesn't work for most of the cases is for that option, every mongoose connection-related object returns dummy object cause not able to access mongodb

I tried to use it like

const server = express();
mongoose.connect('mongodb://localhost/advisorDemoTestDB', { useMongoClient: true })
    .then(() => require('./db-init')(server))
    .catch(err => console.error(err));

but it doesn't work

alanpurple avatar Jun 25 '17 18:06 alanpurple

Having same issue, even without replica sets.

JCMais avatar Jun 25 '17 21:06 JCMais

Having same issue:

(node:4138) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0,
 use `openUri()` instead, or set the `useMongoClient` option if using `connect()` 
or `createConnection()`

bertolo1988 avatar Jun 25 '17 22:06 bertolo1988

Same...

DZuz14 avatar Jun 26 '17 02:06 DZuz14

After I added below code to my mongo connection, I can't query out anything now. {useMongoClient: true}. Any suggestions will be appreciate!!

z1000zhihe avatar Jun 26 '17 08:06 z1000zhihe

+1 same with replica sets, no sharding

angeloLed avatar Jun 26 '17 09:06 angeloLed

+1

niftytushar avatar Jun 26 '17 10:06 niftytushar

+1

evandersar avatar Jun 26 '17 10:06 evandersar

+1

prawee avatar Jun 26 '17 11:06 prawee

+1

heartz66 avatar Jun 26 '17 15:06 heartz66

+1

Yurockkk avatar Jun 26 '17 15:06 Yurockkk

+1

mlyngvo avatar Jun 26 '17 15:06 mlyngvo

+1

Math3v avatar Jun 26 '17 17:06 Math3v

The easiest fix for this; "npm remove mongoose" then "npm install [email protected] --save" problem solved. Upgrading is not always the best option.

ghost avatar Jun 26 '17 17:06 ghost