Using MongoDB in a NodeJS Project

Using MongoDB in a NodeJS Project

MongoDB is a popular NoSQL database widely used in web development for its scalability, flexibility, and ease of use. NodeJS, on the other hand, is a popular server-side JavaScript runtime that allows developers to build fast and scalable network applications.

When used together, MongoDB and NodeJS make a powerful combination for building modern web applications that can handle large amounts of data and traffic. This article will cover the basic process of creating and connecting a MongoDB database to a simple NodeJS Application.

Creating A MongoDB Account

First of all, go to https://www.mongodb.com/ to create an account (log in if you already have one)

Creating A Database

After creating an account/login into your account, you create a cluster: If you have not created a database before, the screen below shows:

Click the Build a database to create a database:

Choose the free option:

Give a name to your cluster (any name that makes sense)

Create a user for the database:

Leave the other options as they are, and click finish and create below.

Once done, the database is created:

Connecting to NodeJS Application:

To follow along, please download a copy of the repo below:

https://github.com/achingachris/airspacenext-server/tree/mongo-db-connect

To link to a NodeJS Application, you will need to get the connection string, by clicking on the connect button:

Choose to connect with your application (drivers) and go to option number 3:

Copy the connection string: Replace the <password> with the password, you created for your database user.

mongodb+srv://doghot:@chrisprojects.bar17ze.mongodb.net/?retryWrites=true&w=majorit

Save the string inside a .env file:

MONGO_URI = mongodb+srv://doghot:@chrisprojects.bar17ze.mongodb.net/?retryWrites=true&w=majority

Connecting Mongo to NodeJs Application:

We will use Mongoose, an object modelling tool for Node.Js, allowing us to create models and schema in our database.

By using Mongoose, developers can define the structure and validation rules for their data, making it easier to manage and maintain the consistency of the data in a MongoDB database. Mongoose simplifies the interaction with MongoDB and helps organise the codebase, making it more efficient and maintainable.

Installing:

npm install mongoose

To connect to MongoDB:

import mongoose from "mongoose";

const connectDB = async () => {
    try {
        const conn = await mongoose.connect(process.env.MONGO_URI, {
        useNewUrlParser: true,
        useUnifiedTopology: true,
        });

        console.log(`MongoDB connected: ${conn.connection.host}`);
    } catch (error) {
        console.error(`Error: ${error.message}`);
        process.exit(1);
    }
}

export default connectDB;

The purpose of the connectDB function is to connect to a MongoDB database using the Mongoose library. Here's a breakdown of the code:

  1. **import mongoose from "mongoose";**: Import the **mongoose** library using ECMAScript Modules (ESM) syntax.

  2. **const connectDB = async () => { ... }**: Declare an asynchronous function named **connectDB**. This function will be responsible for establishing the connection to the MongoDB database.

  3. **try { ... } catch (error) { ... }**: Use a try-catch block to handle any errors while connecting to the MongoDB database.

  4. **const conn = await mongoose.connect(process.env.MONGO_URI, { ... });**: Inside the try block, call the **mongoose.connect()** method with the connection string stored in the **MONGO_URI** environment variable. This is an asynchronous operation, so you use the **await** keyword to wait for the connection to be established. The **connect()** method returns a connection object, which is stored in the **conn** constant.

  5. **useNewUrlParser: true, useUnifiedTopology: true,**: These options are passed to the **mongoose.connect()** method to enable the new URL parser and the unified topology in the MongoDB driver. The new URL parser is less strict and more efficient, while the unified topology provides better support for various MongoDB deployment topologies.

  6. **console.log(**MongoDB connected: ${conn.connection.host}**);**: If the connection is successful, log a message to the console with the host information.

  7. **catch (error) { ... }**The catch block will be executed if an error occurs during the connection attempt.

  8. **console.error(**Error: ${error.message}**);**: Log the error message to the console.

  9. **process.exit(1);**: Terminate the Node.js process with an exit code of 1, indicating that an error occurred.

  10. **export default connectDB;**: Export the **connectDB** function as the default export of this module. This allows you to import and use the **connectDB** function in other parts of your application.

To use this module, you'll need to import it and call the **connectDB()** function in the main entry point of your application. Also, set the **MONGO_URI** environment variable to the appropriate connection string for your MongoDB instance.

Following these simple steps, you can now use MongoDB in your NextJS Project.

Note To Nerds:

This article is a build of my project documentation. I am building a MERN stack E-Commerce to refresh my programming skills and documenting it to refresh my technical writing skills