New to MongoDB - useNewUrlParser,client.db()

Hello.I am a beginner to MongoDB.
My issues are the following :
I can’t understand what useNewUrlParser:true actually does and don’t quite understand how client.db() actually works.I heard it creates a reference to a database but I am actually confused as to what that might mean.

Sorry for the inconvenience.Thanks.

MongoClient.connect(connectionURL,{useNewUrlParser:true},
(error,client)=>{

    if(error) { return console.log(error)};

     console.log("Connected succesfully"); 

   

const db = client.db(databaseName);

});

useNewUrlParser : true is in place because there’s a piece of the MongoDB code that they won’t be using/supporting in the future. Putting this in your code ensures that your code won’t break when that change is eventually made.
There can be many databases within a single MongoDB client. client.db(databaseName) points to the one that you want to use. Just as an example, I might have three databases, “Project1”, “Project2”, and “Project3”. When I say const db = client.db("Project1"), now MongoDB knows that I want to work with the database named “Project1”, and I can do that with db.collection.findOne(etc...). Collections are like tables in a SQL database. Hope that helps!

1 Like

You can check the mongoosejs docs, useNewUrlParser Option for a brief explanation.

MongoClient

useNewUrlParser | boolean | false | optional

Determines whether or not to use the new url parser. Enables the new, spec-compliant, url parser shipped in the core driver. This url parser fixes a number of problems with the original parser, and aims to outright replace that parser in the near future.