DEV Community

KISHAN RAMLAKHAN NISHAD
KISHAN RAMLAKHAN NISHAD

Posted on

How do I find my connection string in Atlas?

To find your MongoDB Atlas connection string, follow these steps:

  1. Log in to MongoDB Atlas
    Go to MongoDB Atlas.
    Sign in with your credentials.

  2. Select Your Cluster
    Click on the Clusters tab.
    Select the cluster you want to connect to.

  3. Get the Connection String

  4. Click on the "Connect" button.

  5. Select "Drivers" or "Connect your application" (if using Node.js).

  6. Choose Node.js as the driver and select the appropriate version.
    Copy the provided connection string (it looks like this):

mongodb+srv://<username>:<password>@cluster0.xxxxx.mongodb.net/<database>?retryWrites=true&w=majority
Enter fullscreen mode Exit fullscreen mode
  1. Replace Placeholders

  2. Replace with your database username.

  3. Replace with your database password.

  4. Replace with the database name you want to connect to.
    Example for Node.js (Mongoose)

const mongoose = require('mongoose');

const uri = "mongodb+srv://yourUser:yourPassword@cluster0.xxxxx.mongodb.net/yourDB?retryWrites=true&w=majority";

mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true })
    .then(() => console.log("Connected to MongoDB Atlas"))
    .catch(err => console.error("Error connecting to MongoDB Atlas:", err));

Enter fullscreen mode Exit fullscreen mode

Top comments (0)