Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 598
Tiago Bértolo Avatar asked Jun 25 '17 19:06

Tiago Bértolo


People also ask

What is useMongoClient?

The useMongoClient option is the source of the ' open() is deprecated in mongoose' deprecation warning that has caused so much discussion. This option opts you in to using Mongoose 4.11's simplified initial connection logic and allows you to avoid getting a deprecation warning from the underyling MongoDB driver.

What is Mongoose connect ()?

Mongoose lets you start using your models immediately, without waiting for mongoose to establish a connection to MongoDB. mongoose. connect('mongodb://localhost:27017/myapp'); const MyModel = mongoose. model('Test', new Schema({ name: String })); // Works MyModel.

Does Mongoose connect return a promise?

You don't handle a Promise with a callback: mongoose call you're callback if provided, otherwise it return the Promise.


2 Answers

This is how you use useMongoClient:

mongoose.connect('mongodb://localhost/advisorDemoTestDB', { useMongoClient: true }) 
like image 167
Xertz Avatar answered Sep 18 '22 13:09

Xertz


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 */ }); 
like image 20
jan basha Avatar answered Sep 20 '22 13:09

jan basha