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?
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.
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.
You don't handle a Promise with a callback: mongoose call you're callback if provided, otherwise it return the Promise.
This is how you use useMongoClient:
mongoose.connect('mongodb://localhost/advisorDemoTestDB', { useMongoClient: true })
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 */ });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With