Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
cosminn777 opened this issue Jun 25, 2017 · 158 comments

Comments

@cosminn777
Copy link

@cosminn777 cosminn777 commented Jun 25, 2017

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

@alanpurple
Copy link

@alanpurple alanpurple commented Jun 25, 2017

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

@tinovyatkin
Copy link

@tinovyatkin tinovyatkin commented Jun 25, 2017

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));
};
@alanpurple
Copy link

@alanpurple alanpurple commented Jun 25, 2017

@tinovyatkin

So then, this is a bug?

@tinovyatkin
Copy link

@tinovyatkin tinovyatkin commented Jun 25, 2017

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)

@hagemann
Copy link

@hagemann hagemann commented Jun 25, 2017

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.

@artnicbr
Copy link

@artnicbr artnicbr commented Jun 25, 2017

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.

@alanpurple
Copy link

@alanpurple alanpurple commented Jun 25, 2017

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

@JCMais
Copy link

@JCMais JCMais commented Jun 25, 2017

Having same issue, even without replica sets.

@bertolo1988
Copy link

@bertolo1988 bertolo1988 commented Jun 25, 2017

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()`
@DZuz14
Copy link

@DZuz14 DZuz14 commented Jun 26, 2017

Same...

@z1000zhihe
Copy link

@z1000zhihe z1000zhihe commented Jun 26, 2017

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

@angeloLed
Copy link

@angeloLed angeloLed commented Jun 26, 2017

+1 same with replica sets, no sharding

@niftytushar
Copy link

@niftytushar niftytushar commented Jun 26, 2017

+1

1 similar comment
@evandersar
Copy link

@evandersar evandersar commented Jun 26, 2017

+1

@prawee
Copy link

@prawee prawee commented Jun 26, 2017

+1

@Heartz66
Copy link

@Heartz66 Heartz66 commented Jun 26, 2017

+1

3 similar comments
@Yurockkk
Copy link

@Yurockkk Yurockkk commented Jun 26, 2017

+1

@IoHL
Copy link

@IoHL IoHL commented Jun 26, 2017

+1

@Math3v
Copy link

@Math3v Math3v commented Jun 26, 2017

+1

@Chenz62
Copy link

@Chenz62 Chenz62 commented Jun 26, 2017

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

@toddmurphy
Copy link

@toddmurphy toddmurphy commented Jun 26, 2017

+1

1 similar comment
@Systerr
Copy link

@Systerr Systerr commented Jun 26, 2017

+1

@plhosk
Copy link

@plhosk plhosk commented Jun 26, 2017

I get 2 messages:

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

Server started on port 3000

Db.prototype.authenticate method will no longer be available in the next major
release 3.x as MongoDB 3.6 will only allow auth against users in the admin db
and will no longer allow multiple credentials on a socket. Please authenticate
using MongoClient.connect with auth credentials.

The first error appeared in 4.11.0. The second error also appeared in the previous version of Mongoose.

@cosminn777
Copy link
Author

@cosminn777 cosminn777 commented Jun 26, 2017

plhosk, the second one should have been fixed in 4.11 but looks like it's still there, for me too.

@jlobos
Copy link

@jlobos jlobos commented Jun 26, 2017

+1

@Alhern
Copy link

@Alhern Alhern commented Jun 26, 2017

+1 sadly.

@DellAnderson
Copy link

@DellAnderson DellAnderson commented Jun 26, 2017

Installed 4.10.8 without issues. Please consider changing npm install mongoose --save to default to 4.10.8 until 11 is stable.

@nicholaai
Copy link

@nicholaai nicholaai commented Jun 27, 2017

+1

@chrisdothtml
Copy link

@chrisdothtml chrisdothtml commented Aug 4, 2017

@iamdubx Sounds like you don't understand the difference between a warning and an error.

The warning in question does not break anything, but warns the user of something that will be removed in the next major release.

Mongoose is horrible.

So keep in mind, you're having this reaction to the developers warning you ahead of time before they remove a feature. I'd also urge you to remember that you're using this free of charge, so maybe give the developers the benefit of the doubt before you throw a tantrum

@bertolo1988
Copy link

@bertolo1988 bertolo1988 commented Aug 4, 2017

@chrisdothtml

The warning in question does not break anything, but warns the user of something that will be removed in the next major release.

It does pollute tests and console which is mildly annoying.

Mongoose is horrible. Also worst documentation site that I saw in libraries that so popular. What a shame!

He is being impolite which is also mildly annoying but he makes a very valid point in regards to Mongoose documentation. It is not the worst documentation ever but it could really be improved.

The fact that mongoose forwards the mongo client API. which works in a very different way, makes Mongoose usage and documentation confusing.

A possible solution for this would be:

  • stop doing it
  • remove the documentation for the Mongo client methods
  • provide a way to share connection between the official client and Mongoose

I would also suggest improving docs searchability by breaking it in several pages and giving more insight to new users about on what is Mongoose actually doing to achieve some of the results.

@jameskentTX
Copy link

@jameskentTX jameskentTX commented Aug 7, 2017

I had this error, but it disappeared after I added {useMongoClient: true} option.
I use Debian 9, testing version, with MongoDb ver. 3.2.11 and mongoose ver. 4.10.4.
I have 'Connected to MongoDB' message (view code in Typescript below), everything works perfectly.

(< any >mongoose).Promise = global.Promise;
mongoose.connect(process.env.MONGODB_URI, {useMongoClient: true});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => {
console.log('Connected to MongoDB');
setRoutes(app);
app.get('/*', function(req, res) {
res.sendFile(path.join(__dirname, '../public/index.html'));
});
app.listen(app.get('port'), () => {
console.log('MyApp listening on port ' + app.get('port'));
});
});

@karanpepi
Copy link

@karanpepi karanpepi commented Aug 9, 2017

Just uninstall the current mongoose version which you have and install the lower version npm install mongoose@4.10.8 --save . No need to do anything else this will definately work for you.

@adrianwebman
Copy link

@adrianwebman adrianwebman commented Aug 13, 2017

mongoose.connect(database.host, { useMongoClient: true });

This solved my problem. But I had another warning coming up after I made this change.

DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html

@bricss
Copy link

@bricss bricss commented Aug 13, 2017

This code solves all deprecation warnings:

mongoose.Promise = global.Promise;
mongoose.connect(uri, {
  keepAlive: true,
  reconnectTries: Number.MAX_VALUE,
  useMongoClient: true
});

More info by the link -> http://mongoosejs.com/docs/connections.html#use-mongo-client

@artnicbr
Copy link

@artnicbr artnicbr commented Aug 15, 2017

@bricss Fantastic! Thanks!

@kranz
Copy link

@kranz kranz commented Aug 15, 2017

@bricss You made my day.

@jpetitte
Copy link

@jpetitte jpetitte commented Aug 15, 2017

someone earned a hero badge...

edit: @bricss It eliminated the deprecation warnings for me, but I couldn't retrieve data from my mLab sandboxes. It may be because they are using an older version of MongoDB. I'll try to check into it later.

edit: @bricss It's now working correctly with the latest mongoose 4.11.9 and a connection to Cosmos DB on Azure.

@adrianwebman
Copy link

@adrianwebman adrianwebman commented Aug 16, 2017

@bricss : Nicely done Bricks! It worked for me even though I'm using a really old version of mongodb in my local 👍

@CodebyOmar
Copy link

@CodebyOmar CodebyOmar commented Aug 16, 2017

@bricss Awesowe! But you might consider putting a smaller number for "reconnectTries". You don't want to spend the whole day reconnecting if there is an issue with your DB.

@nikkwong
Copy link

@nikkwong nikkwong commented Aug 19, 2017

@afoke 's solution worked for me, only when i explicitly declare the db in the connection string i.e. mongodb://localhost:27017/test but not mongodb://localhost:27017

@aprisniak
Copy link

@aprisniak aprisniak commented Aug 21, 2017

May I create connection to 2 DB's without this warning? I need to use my models with different DB's.

nip10 pushed a commit to nip10/todo-api that referenced this issue Aug 24, 2017
Node was throwing a deprecation error related to mongoose. This is a known bug (Automattic/mongoose#5399) so I used a work-around to avoid it.
@moolsbytheway
Copy link

@moolsbytheway moolsbytheway commented Aug 27, 2017

Using mongoose v^4.11.6 , when added useMongoClient the warning dispeared.

const uri = "http://blablabla.blo/blaDB"; mongoose.Promise = global.Promise; mongoose.connection.on('error', (err) => { console.error(Mongoose connection error: ${err}`);
process.exit(1);
});
mongoose.connect(uri, {useMongoClient: true});

 // load models
 require('./model1');
 // ...
 require('./modelN');`
yagisuke added a commit to yagisuke/GraphQL that referenced this issue Sep 4, 2017
@amado-saladino
Copy link

@amado-saladino amado-saladino commented Sep 15, 2017

connecting to database in this way solves the problem:
mongoose.connect(url,{user:'username',pass:'mypassword',useMongoClient:true});

@iroshanAV
Copy link

@iroshanAV iroshanAV commented Sep 17, 2017

Will these errors cause problems , for example in security?

@RxAssim
Copy link

@RxAssim RxAssim commented Sep 17, 2017

No more "deprecated" warning pops up if I use this syntax 🎉

mongoose.connection.openUri('mongodb://localhost/test')
  .once('open', () => console.log('Good to go !'))
  .on('error', (error) => {
    console.warn('Warning', error);
  });
@mitesh-solulab
Copy link

@mitesh-solulab mitesh-solulab commented Sep 21, 2017

I faced the same deprecation warning as below:
(node:2300) DeprecationWarning: open() is deprecated in mongoose >= 4.11.0, use openUri() instead, or set the useMongoClient option if using connect() or createConnection(). See http://mongoosejs.com/docs/connections.html#use-mongo-client

Solution

I tried the following and it works fine. Gives no deprecation warnings.

mongoose.connect('mongodb://127.0.0.1:27017/your-database-name', { useMongoClient: true, promiseLibrary: global.Promise });

Hope this will help everyone facing this issue.

@MichFe
Copy link

@MichFe MichFe commented Sep 23, 2017

I have the same issue but when trying to connect to Mongo Atlas service it just not connect with {useMongoClient:true} option and without it the connection is no longer executed.

Anyone with the same issue?

@mominbutt12
Copy link

@mominbutt12 mominbutt12 commented Oct 1, 2017

I had solved my problem by just change the mongoose.connect to mongoose.createConnection

mongoose.createConnection(config.uri, (err) => {
if (err) {
console.log('Could NOT connect to database: ', err);
} else {
console.log('Connected to database: ' + config.db);
}
});

@stanisdev
Copy link

@stanisdev stanisdev commented Oct 6, 2017

I solved this problem like that (Mongoose 4.12.0, NodeJS - 7.10.1)

mongoose.Promise = require('bluebird');
mongoose.connect('mongodb://localhost:27017/books_exchange', {
  useMongoClient: true,
  promiseLibrary: require('bluebird')
}).then(() => {
  var userSchema = new mongoose.Schema({
    name: String,
    password: String
  });
  var User = mongoose.model('User', userSchema);

  var person = new User({
    name: "John",
    password: "qwerty"
  });

  person.save().then(() => {
    console.log('Data saved');
  }).catch(e => {
    console.log(e);
  });
}).catch(e => {
  console.log('Error while DB connecting');
  console.log(e);
});
@Automattic Automattic locked and limited conversation to collaborators Oct 6, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
You can’t perform that action at this time.