Getting Started with MongoDB

Isuru Jayalath
Enlear Academy
Published in
4 min readJan 12, 2024

--

Photo by Rubaitul Azad on Unsplash

When it comes to database management systems, MongoDB stands out as a powerful and flexible NoSQL solution. If you’re considering integrating MongoDB into your application, the first question that might come to mind is, “How can I get a MongoDB database?” In this blog post, we’ll explore various methods and platforms that allow you to access a MongoDB database and how to connect it with your backend.

1. MongoDB Atlas — The Cloud Platform:

MongoDB Atlas is MongoDB’s fully managed cloud database service, providing an easy and efficient way to deploy, manage, and scale MongoDB databases. There are special benefits of using MongoDB Atlas, including automatic backups, global clusters, and seamless integration with major cloud providers such as AWS, Azure, and GCP.

2. Local Installation — A Hands-On Approach:

Local installation refers to the process of setting up and running a MongoDB database directly on your local machine for development and testing purposes. This approach is particularly useful when you are working on small-scale projects or during the initial stages of application development.

3. Cloud-based Platforms — Managed Services:

Services like Amazon DocumentDB on AWS can serve as fully managed alternatives to MongoDB, allowing you to leverage cloud scalability and infrastructure. Users can benefit from features like automatic backups, global clusters, and effortless scaling on AWS, making it an ideal choice for applications with varying workloads. The integration simplifies database deployment and management, allowing developers to focus on building robust applications without the burden of manual maintenance.

4.Docker — Containerize Your Database:

Containerization involves packaging an application and its dependencies into a lightweight, isolated container, ensuring consistency and portability. This section guides users through the process of creating a Docker container for MongoDB, allowing them to isolate the database environment and run it seamlessly on any system that supports Docker. This approach not only promotes consistency in database setups but also facilitates easier collaboration and deployment workflows within a containerized ecosystem.

MongoDB Atlas stands out as a better option due to its fully managed approach, global scalability, seamless cloud integration, security features, user-friendly interface, free tier for development, and strong community support. So let’s walk through the steps of getting MongoDB with MongoDB Atlas.

First, go to https://www.mongodb.com/ and create a MongoDB account. Create a New Cluster by choosing a cloud provider (AWS, Azure, or GCP) and a region for your cluster.

After that go to the quick start option In security and create a user by entering a username and password to access the database. Then add your current IP address from the network access. Now it is time to connect the database to the backend.

Now go to the dashboard and click the connect option. Then choose the drivers option and select your driver and version. ( Here I have used nodejs and version 5.5)

After that copy the MongoDB URI and go to your backend development environment. Let’s see how you can connect the MongoDB database to the backend.

You can create a separate JavaScript file in your folder structure to establish the connection. Eg: connection.js

( Note that here I am using a NodeJS backend )

const mongoose = require("mongoose");

This line imports the Mongoose library, which is a popular ODM (Object Data Modeling) library for MongoDB and Node.js. It provides a straight-forward, schema-based solution for modeling application data.

const monogdbURI = "mongodb+srv://<username>:<password>@cluster0.kijedwg.mongodb.net/?retryWrites=true&w=majority";

Here, you define the MongoDB connection URI. It includes the username, password, and the cluster details . This URI is specific to MongoDB Atlas, MongoDB's cloud service.

mongoose.connect(monogdbURI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});

This code initiates a connection to MongoDB using Mongoose. The connect method takes the MongoDB connection URL and an options object.

const connection = mongoose.connection;

This line retrieves the connection object from Mongoose. It provides access to various events and methods related to the MongoDB connection

connection.once("open", () => {
console.log("MongoDB connected!");
});

This code sets up an event listener for the “open” event. When the MongoDB connection is successfully established, this callback function is executed, logging a message to the console indicating that the MongoDB connection is open.

Now armed with the knowledge of MongoDB connection in a Node.js backend, you can start building applications that leverage the flexibility and scalability of this NoSQL database. As you dive deeper into MongoDB’s capabilities, keep exploring, innovating, and creating solutions that elevate your application to new heights. Happy coding!

--

--

Undergraduate of University of Moratuwa, Faculty of Information Technology.