MongoDB is a NoSQL, general purpose, document based, distributed database. Document based means it stores data in JSON-like documents.
In this article we're gonna set-up a free tier MongoDB cluster on MongoDB cloud and connect with our NodeJS & Express application.
Launching the MongoDB Cluster
So to get Started, let's go to the MongoDB HomePage and click on Start Free button on the center of the screen.
Once, You click on Start Free, You'll be presented with a form similar to this:
Fill up the required fields and provide a valid Email Address and click on "Get Started Free". Afterwards you'll be logged into the Mongo Cloud Console and will be prompted to Create a Cluster. It will be something similar to :
Now, just rename your cluster and and click on "Create Cluster". This will kick the cluster launch process. After 5 minutes or so the cluster will be ready and you'll be allowed to create User.
Creating DB User
Now, from the left nav menu, click on "Database Access". You'll have something similar to this
Now, click on "Add New Database User". You'll be prompted with something similar to
Enter the username and password in the respective field and click on Add User.
WhiteListing IP Address
Once the Database User has been created, You'll need to Whitelist the IP address through which DB can be accessed. To do so, Go back to Clusters from the Left Nav-bar and click on CONNECT in the SANDBOX area.
Now, choose Allow Access From Anywhere and save. Then click on Choose a Connection Method.
Getting Connection String
Now, after clicking on Choose Connection Method, You'll be presented with following options
Choose "Connect Your Application" and you'll be provided with following
Copy the connection String by clicking on Copy and paste it on a NotePad file. It would look something like :
mongodb+srv://dmr:<password>@mytestcluster.f5kbx.mongodb.net/<dbname>?retryWrites=true&w=majority
Replace the with the password you used when creating the user and replace with anything you'd like. In my case i'd use testdb.
Connecting with Our Express Application
Now, with our connection string, we can directly make the connection with our DB using MongoDB Compass but We'd like to connect our Express application with MongoDB. So create a folder called MongoTest and via terminal navigate to the folder.
Considering You're in the Mongotest folder, type the following commands:
npm init -y
npm i express mongoose --save
touch index.js
We're initializing package.json file and installing Express and Mongoose packages.
Now, lets write our Index.js file. It would be something similar to
const express = require("express");
const mongoose = require("mongoose");
const app = express();
let db =
"mongodb+srv://dmr:<Password>@mytestcluster.f5kbx.mongodb.net/testdb?retryWrites=true&w=majority";
mongoose
.connect(db, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("mongoDB connected Successfully"))
.catch((err) => console.log(err));
app.listen(3000, console.log("Server running on port 3000"));
Remember to replace the section with the Password you set for the user. Now, just type
node index.js
If you've done everything correctly, You'll see the Prompt MongoDB Connected Successfully.
Thanks for reading. In the upcoming Article, i'll be performing MongoDB CRUD operations too using NodeJS, Express and mongoose.
Top comments (2)
Great content. I can recommend mongodb.com/products/tools/compass for managing MongoDB.
thanks , it was very useful