Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (47.6k points)

I have a database wrapper class that establishes a connection to some MongoDB instance:

async connect(connectionString: string): Promise<void> { 

this.client = await MongoClient.connect(connectionString) 

this.db = this.client.db() 

}

This gave me a warning:

(node:4833) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.

The connect() method accepts a MongoClientOptions instance as second argument. But it doesn't have a property called useNewUrlParser. I also tried to set those property in the connection string like this: mongodb://127.0.0.1/my-db?useNewUrlParser=true but it has no effect on those warning.

So how can I set useNewUrlParser to remove those warning? This is important to me since the script should run as cron and those warnings result in trash-mail spam.

I'm using mongodb driver in version 3.1.0-beta4 with corresponding @types/mongodb package in 3.0.18. Both of them are the latest available using npm install.

Workaround

Using an older version of MongoDB driver:

"mongodb": "~3.0.8", "@types/mongodb": "~3.0.18"

1 Answer

0 votes
by (106k points)

You can see the below-highlighted code to mongoose connection solved the warning for mongoose driver

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

Related questions

0 votes
2 answers
asked Sep 13, 2019 in SQL by Sammy (47.6k points)
0 votes
2 answers
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Sep 6, 2019 in SQL by Sammy (47.6k points)

Browse Categories

...