How to Easily Connect to DigitalOcean's Managed MongoDB
Tags: mongodb, digitalocean, database
Published: 2022-06-07 21:36:48

---
title: How to Easily Connect to DigitalOcean's Managed MongoDB
published: true
description: Learn how to easily connect to your managed MongoDB instance on DigitalOcean.
tags: mongodb, digitalocean, database
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
---

So you've just created a new MongoDB database using [DigitalOcean's managed databases](https://www.digitalocean.com/products/managed-databases-mongodb) and you're wondering how to connect it to your application. Whether you're building a new app, adding new features to an existing one, or just want to access your newly created database cluster, it has never been easier to connect to your MongoDB database on DigitalOcean. 

Simply copy the connection string provided in the Connection Details section of the dashboard and remember to replace the password field with your password and you're off to the races.

![Connection String](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gnx7c6g93zwkxdj45ir9.png)

If you forgot or don't have your password on hand, you can generate a new one in the Users and Databases tab by clicking on the More Options menu and hitting the Reset Password button. You'll need to provide your username and then you will be able to reveal the newly generated password.

![Reset password](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d472an7czoe6gh9ihjnc.png)

Make a note of your password as you need it in just a little bit. Let's connect our MongoDB database to the official MongoDB GUI tool, [MongoDB Compass](https://www.mongodb.com/products/compass). I'll open up Compass and in the new connection card I'll add my connection string, add the password and hit Connect. If all is right, I'll be connected and can view, edit, and manipulate data in my MongoDB database.

![MongoDB Compass](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p10z0yv3avwleosawxdf.png)

The same process of connecting can be applied to our applications. I have a simple NodeJS application that is going to connect to our database cluster using the MongoDB NodeJS driver and will write some data to a database called `test` in a file called `index.js`.

```js
const { MongoClient } = require("mongodb");

let client = new MongoClient("mongodb+srv://doadmin:{REPLACE-WITH-YOUR-PASSWORD}@db-mongodb-sfo3-18734-0eb71eca.mongo.ondigitalocean.com/admin?tls=true&authSource=admin&replicaSet=db-mongodb-sfo3-18734", {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

client.connect().then((client) => {
  db = client.db("test");

  db.collection("test").insertOne({ message:"Hello from DigitalOcean!"}).then(()=>{
    process.exit();
  });

});
```

Just like with the Compass example, we just need to provide the connection string and we'll be good to go. Let's see it in action.

To run the app, from your terminal window execute the following command: `node index.js`. The app will connect to MongoDB, insert a document in the `test` collection, inside of a database called `test`, and then exit.

Going back to Compass and refreshing our database, we can see the newly added data. That's it! Now that you know how to connect your DigitalOcean MongoDB cluster to your applications, I can't wait to see what you will build.

![MongoDB Compass data](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i901nvjezf458t1lmheo.png)

Good luck, luck and happy coding.

You can watch a video walkthrough of the above tutorial below:

{% youtube zUc9xVhoFss %}