Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (50.2k points)

I have connected to my database, with the help of the following code:

private connectDatabase(databaseUri: string): Promise<Mongoose.Connection> {

    return Mongoose.connect(databaseUri).then(() => {

        debug('Connected to MongoDB at %O', databaseUri);

        return Mongoose.connection;

    });

}

After that when I update my Mongoose to version 4.11.0, I got the following warning while 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()`

Can you guide me on how to set the useMongoClient that is mentioned in the warning message?

1 Answer

0 votes
by (107k points)
edited by

You just have to set your useMongoClient as true to connect to your MongoDB server. If you are taking mongoose.connect() as your function then just refer the following code:

const mongoose = require('mongoose');

mongoose.Promise = global.Promise;

// Connect to MongoDB on localhost:27017

mongoose.connect('mongodb://localhost:27017/test', { useMongoClient: true });

// Create a model and insert a new doc

const Test = mongoose.model('Test', new mongoose.Schema({ name: String }));

Test.create({ name: 'Val' }).then(doc => console.log(doc));

If you are a beginner and want to know more about MongoDB, then check out this MongoDB tutorial by Intellipaat which will teach you MongoDB from basics.

Related questions

+1 vote
2 answers
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...