DEV Community

Memel Meless
Memel Meless

Posted on

Test your MongoDB connectivity with mongoose

I was building an app in which I need to switch database from the UI. In that situation we need to update the parameters of connexion and validate them, we need to test if the connexion can be etablished.

using mongoose

$ npm install mongoose
Enter fullscreen mode Exit fullscreen mode

In your testing code file

const mongoose = require("mongoose");
const server = "127.0.0.1";
const authDb = "admin";
const database = "purgenie";
const user = "memel";
const password = "yourP@ssword";
const port = "127017";

let options  = {};
password = encodeURIComponent(password);
const uri = "mongodb://"+user+":"+password+"@"+server+ ":" +port+"/"+database+"?authSource="+authDb;

mongoose.createConnection(uri, options).then(()=>{
    console.log("Connexion succeed");
}).catch((err) =>{
    console.error(err);
});

Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
smaranh profile image
Smaran Harihar

Do we need to always need username and password to play with mongoose? I am creating a practise app and was wondering with I can use mongoose without the username and password on my local DEV environment?