Share
Explore

Creating a MongoDB Atlas database to use with a Node.js program in Visual Studio Code.

image.png
image.png
image.png

Here's a step-by-step lab workbook for creating a MongoDB Atlas database to use with a Node.js program in Visual Studio Code.
Prerequisites: ​1. Sign Up or login to the MongoDB Atlas platform 2. Install Node.js and npm(Node Package Manager) 3. Install Visual Studio Code
### Instructions:

Create a Cluster in your MONGO DB Atlas Account:

megaphone

To create a MongoDB Atlas cluster using the webpage GUI, you need to first log in to your Atlas account. Once you are logged in, you can create an Atlas organization and then create a project in this organization. You will deploy your first cluster in this project. After creating the project, you can proceed to deploy a free cluster by following these steps:

Click on the "Build a Cluster" button on the project dashboard.
Select the cloud provider and region where you want to deploy your cluster.
Choose the cluster tier that you want to use.
Configure the additional settings for your cluster, such as the cluster name, the number of nodes, and the disk size.
Click on the "Create Cluster" button to create your cluster.
Once you have created your cluster, you can connect to it using the connection string provided in the Atlas UI.
Further reading:


## 1. Setting up MongoDB Atlas
1.1. In MongoDB Atlas, click on the "Clusters" option from the left navigation pane.
1.2. Click "Build a Cluster" button.
1.3. Choose a Provider and Region to meet your requirements and "Create Cluster".
1.4. Click on the "CONNECT" button for your newly created cluster.
1.5. In the connect dialog, click on "Add Your Current IP Address" then choose a username and password for your Database Access. Click "Create Database User".
1.6. In the "Choose a connection method" section, select "Connect your application", then in the "Connection String Only" field, copy the provided connection string, it is needed for connecting your Node.js app to the MongoDB Atlas.
Attention: Replace in the connection string `<password>` with the password you set up earlier for your Database User.
## 2. Initialize Node.js Project in Visual Studio Code
2.1. Create a new folder in your local system, open Visual Studio Code and open that folder.
2.2. Open Terminal (Terminal > New Terminal) and navigate to the new folder.
2.3. Initialize a new Node.js project by typing:
```bash npm init -y ```
2.4. This command creates a new `package.json` file.
## 3. Install necessary Node.js Packages
3.1. For this project, you will need MongoDB Node.JS Driver, an official MongoDB driver for Node.js. Install it with this command:
```bash npm install mongodb --save ```
## 4. Create Your Node.js Application
4.1. In Visual Studio Code, create a new file "dbtest.js" in your project folder.
4.2. In "dbtest.js", copy and paste this code:
```javascript const MongoClient = require('mongodb').MongoClient; const uri = "mongodb+srv://<username>:<password>@cluster0.mongodb.net/<dbname>?retryWrites=true&w=majority"; const client = new MongoClient(uri, { useNewUrlParser: true }); client.connect(err => { const collection = client.db("test").collection("devices"); // perform actions on the collection object client.close(); }); ```
4.3. Update your uri variable with the actual connection string previously copied from MongoDB Atlas. Replace `<username>`, `<password>`, and `<dbname>` placeholder with correct values.
## 5. Test Your Database Connection
5.1. Use VS Code's terminal to run your Node.js application
```bash node dbtest.js ```
5.2. If there is no error logged, it means your MongoDB database in MongoDB Atlas has been successfully connected with your Node.js application.
Remember, with your database now set up and connected to your project, you can perform CRUD operations and many other tasks. Consult the MongoDB Node.js driver documentation to learn more about the operations you can perform on your MongoDB database.
If the terminal displays an error, double-check your uri connection string and ensure it contains correct username, password, and dbname.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.