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

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 commented Jun 25, 2017

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

@tinovyatkin
Copy link

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

@tinovyatkin

So then, this is a bug?

@tinovyatkin
Copy link

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

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

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 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 commented Jun 25, 2017

Having same issue, even without replica sets.

@bertolo1988
Copy link

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 commented Jun 26, 2017

Same...

@z1000zhihe
Copy link

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

+1 same with replica sets, no sharding

@niftytushar
Copy link

+1

1 similar comment
@evandersar
Copy link

+1

@prawee
Copy link

prawee commented Jun 26, 2017

+1

4 similar comments
@heartz66
Copy link

+1

@Yurockkk
Copy link

+1

@mlyngvo
Copy link

mlyngvo commented Jun 26, 2017

+1

@Math3v
Copy link

Math3v commented Jun 26, 2017

+1

@ghost
Copy link

ghost 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

+1

1 similar comment
@Systerr
Copy link

Systerr commented Jun 26, 2017

+1

@plhosk
Copy link

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

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 commented Jun 26, 2017

+1

@Alhern
Copy link

Alhern commented Jun 26, 2017

+1 sadly.

@DellAnderson
Copy link

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

+1

1 similar comment
@montoias
Copy link

+1

@chrisdothtml
Copy link

@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 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 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

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 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 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

@bricss Fantastic! Thanks!

@kranz
Copy link

kranz commented Aug 15, 2017

@bricss You made my day.

@jpetitte
Copy link

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

@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

@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

@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

@ghost
Copy link

ghost 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 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

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

@iroshanAV
Copy link

Will these errors cause problems , for example in security?

@assimelha
Copy link

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

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 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

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);
}
});

@vkarpov15 vkarpov15 removed backwards-breaking discussion If you have any thoughts or comments on this issue, please share them! labels Oct 2, 2017
@stanisdev
Copy link

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.
Labels
None yet
Projects
None yet
Development

No branches or pull requests