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 avaliable using npm install
.
Using an older version of mongodb driver:
"mongodb": "~3.0.8",
"@types/mongodb": "~3.0.18"
Check your mongo
version:
mongo --version
If you are using version >= 3.1.0, change your mongo
connection file to ->
MongoClient.connect("mongodb://localhost:27017/YourDB", { useNewUrlParser: true })
or your mongoose connection file to ->
mongoose.connect("mongodb://localhost:27017/YourDB", { useNewUrlParser: true });
Ideally, it's a version 4 feature, but v3.1.0 and above are supporting it too. Check out MongoDB GitHub for details.