DEV Community

Memel Meless
Memel Meless

Posted on

1 1

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?

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay