Tiago Bértolo
Tiago Bértolo

Reputation: 4333

How to set useMongoClient (Mongoose 4.11.0)?

This is the code I use to connect to my database:

private connectDatabase(databaseUri: string): Promise<Mongoose.Connection> {
    return Mongoose.connect(databaseUri).then(() => {
        debug('Connected to MongoDB at %O', databaseUri);
        return Mongoose.connection;
    });
}

Today I updated Mongoose to version 4.11.0 and I got this warning when running my tests:

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

I can't find any information on how to "set useMongoClient".

Do you guys know how to?

Upvotes: 54

Views: 42808

Answers (12)

mohamed ibrahim
mohamed ibrahim

Reputation: 583

Upgrade mongoose , first you need to uninstall it with this code:

npm uninstall mongoose

Then install it again with the following code :

npm install mongoose  --save

Upvotes: -1

Xertz
Xertz

Reputation: 944

This is how you use useMongoClient:

mongoose.connect('mongodb://localhost/advisorDemoTestDB', { useMongoClient: true })

Upvotes: 78

Abhishek Gupta
Abhishek Gupta

Reputation: 4166

As many answers say, adding { useMongoClient: true } in options argument to connect or createConnection method will solve the problem.

For e.g.

// With mongoose.connect method
mongoose.connect('mongodb://localhost/app', { useMongoClient: true });

// With createConnection method
mongoose.createConnection('mongodb://localhost/app', { useMongoClient: true });

But what is MongoClient in the first place?

From MongoDB Node.js Driver version 1.2, a new connection class MongoClient was introduced that has the same name across all the official drivers.

The new connection class MongoClient acknowledges all writes to MongoDB, in contrast to the existing DB connection class which has acknowledgements turned off.

So { useMongoClient: true } tells moongoose to use the new connection class rather than the old one

For more info click here

Upvotes: 6

Raja Parivesh
Raja Parivesh

Reputation: 626

The easiest fix for this:

Run this command in project root folder via terminal:

npm remove mongoose

npm install [email protected] --save

Problem solved.

Upgrading is not always the best option.

https://github.com/Automattic/mongoose/issues/5399

Upvotes: 1

jan basha
jan basha

Reputation: 291

Add { useMongoClient: true } as another argument to connect or createConnection method, its depends on version of mongoose you are using.

// Using `mongoose.connect`...
var promise = mongoose.connect('mongodb://localhost/myapp', {
  useMongoClient: true,
  /* other options */
});
// Or `createConnection`
var promise = mongoose.createConnection('mongodb://localhost/myapp', {
  useMongoClient: true,
  /* other options */
});

Upvotes: 29

Boniface Dennis
Boniface Dennis

Reputation: 49

Error:

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

Solution: To set useMongoClient:true in connection property

var promise = mongoose.connect('mongodb://localhost/myapp', {
  useMongoClient: true,
  /* other options */
});

See http://mongoosejs.com/docs/connections.html#use-mongo-client now listening for request

Upvotes: 0

Aditya Parmar
Aditya Parmar

Reputation: 1179

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/starbucks', { 
    useMongoClient: true 
});
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('openUri', function() {
    // we're connected!
});

Upvotes: 0

Tiago B&#233;rtolo
Tiago B&#233;rtolo

Reputation: 4333

Without Typescript you can pretty much ignore the issue and use Mongoose.connect(databaseUri, { useMongoClient: true }).

If you really want to avoid having the warning avoid the version 4.11.0.

I updated to version 4.11.1 and since @types/[email protected] are not yet updated and they do not mention the field useMongoClient in the ConnectionOptions, this is how i imported the module:

const Mongoose = require('mongoose');

And then used this function:

private connectDatabase(databaseUri: string): Promise<any> {
    return Mongoose.connect(databaseUri, { useMongoClient: true })
        .then(() => {
            console.log('Connected to MongoDB at ', databaseUri);
            return Mongoose.connection;
        })
        .catch(err => debug(`Database connection error: ${err.message}`));
}

Upvotes: 7

krl
krl

Reputation: 5296

Connect to MongoDB with Mongoose 4.11.x (tested with both mLab single instance and MongoDB Atlas replica set):

const mongoose = require('mongoose');

mongoose.Promise = global.Promise;

const options = {
  promiseLibrary: global.Promise,
  useMongoClient: true,
};

function connect() {
  mongoose.connect(URI, options)
    .then(function() {
      const admin = new mongoose.mongo.Admin(mongoose.connection.db);
      admin.buildInfo(function(err, info) {
        if (err) {
          console.err(`Error getting MongoDB info: ${err}`);
        } else {
          console.log(`Connection to MongoDB (version ${info.version}) opened successfully!`);
        }
      });
    })
    .catch((err) => console.error(`Error connecting to MongoDB: ${err}`));
}

module.exports = connect;

Create model:

const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({...});

module.exports = mongoose.model('User', userSchema);

Upvotes: 4

James Choi
James Choi

Reputation: 1128

According to the mongoose documentation, this is how useMongoClient can be set.

function connectDatabase(databaseUri){
    var promise = mongoose.connect('mongodb://localhost/myapp', {
        useMongoClient: true,
    });
    return promise;
}

Upvotes: 1

yogesh chatrola
yogesh chatrola

Reputation: 431

The easiest fix for this would be to open your terminal, change your directory to your root project (folder where package.json is)

Run:
npm remove mongoose

then:

npm install [email protected] --save

problem solved.

Upgrading is not always the best option.

http://mongoosejs.com/docs/connections.html

Upvotes: 10

Crisam De gracia
Crisam De gracia

Reputation: 163

mongoose.connection.openUri('mongodb://127.0.0.1/camp_v12')

has anyone tried this? my deprecated warning disappeared when i use this, it was from the documentation

http://mongoosejs.com/docs/connections.html

Upvotes: 12

Related Questions