Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect [duplicate]

Tags:

node.js

When I run a simple NodeJS project to upload data to a database using MongoDB, I get the following errors:

(node:3556) 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.

Why? and how can I fix this?

like image 574
Sourab Bhowmik Avatar asked Jul 29 '18 18:07

Sourab Bhowmik


People also ask

What does useNewUrlParser true do?

The useNewUrlParser OptionTo use the new parser, pass option { useNewUrlParser: true } to MongoClient. connect. The MongoDB Node. js driver rewrote the tool it uses to parse MongoDB connection strings.

What is useUnifiedTopology true?

useUnifiedTopology: False by default. Set to true to opt in to using the MongoDB driver's new connection management engine. You should set this option to true , except for the unlikely case that it prevents you from maintaining a stable connection.

How does MongoDB connect to node js database?

To connect a Node. js application to MongoDB, we have to use a library called Mongoose. mongoose. connect("mongodb://localhost:27017/collectionName", { useNewUrlParser: true, useUnifiedTopology: true });


2 Answers

If you're using mongo to connect try using:

MongoClient.connect('mongodb://user:[email protected]:port/dbname', { useNewUrlParser: true });

If you're using mongoose, use something like this:

mongoose.connect('mongodb://user:[email protected]:27017/dbname', { useNewUrlParser: true });

You could also use something like this:

const config = {
  autoIndex: false,
  useNewUrlParser: true,
};
return mongoose.connect(uri, config);

As explained by lineus:

https://github.com/Automattic/mongoose/issues/6667

like image 175
Cisko Rijken Avatar answered Nov 15 '22 08:11

Cisko Rijken


Put this code inside your layout file

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/yourDatabase', { useNewUrlParser: true });
var Schema = mongoose.Schema;

Then you can create your Schema layout, for example:

var mySchema = new Schema({
    first_name: String, 
    last_name: String
});
like image 40
Mario Minondo Avatar answered Nov 15 '22 07:11

Mario Minondo