Create MongoDB database and connect with Mongoose

Aneeqa Khan - Dec 16 '22 - - Dev Community

Table Of Contents

For this series, I'm following an excellent video tutorial from Traversy Media

Create MongoDB Database

First, go to the MongoDB site and signup. There is a signup from google or GitHub option as well.
After signup, create your organization to continue.

  1. Click Create an organization button.
  2. Type your organization name and select MongoDB Atlas and press next.
  3. You don't need to add any members and permissions. Just click Create organization button.

Now we need to create a project.

  1. Click New project button.
  2. Type name of project todos and click next.
  3. And press Create Project button.

Build a Database Cluster

After creating a database, you can choose a cloud provider and other specs.

  • Click the Build a Database button and choose the Shared (free) option. It is suitable for learning purposes and small projects.
  • On the next page, you can select the cluster provider, region, and other settings. But I am keeping everything as default and only updating the name of the cluster.
  • So I typed the "TodosCluster" name and pressed Create Cluster button.

cluster-name

  • Next, create a username and password to have read and write access to your database. Enter username and password and click Create User button.

user-mongodb

  • And after that, add your IP address to connect with your cluster in the local environment.

ip-address

  • Finally, press the Finish and Close button.

Before connecting the database with our project, let's quickly create a todos collection.

  1. Go to the Collections tab and press Add my own data button.
  2. Type todosapp as database name and todos as collection name and press Create.

Connect with Mongoose

  • Go to the overview tab and click the connect button.
  • Select Connect your application option and copy the connection string.
  • Go to the .env file and add the MONOGO_URI variable and paste the connection string there.
  • Replace << password >> with your db password and type your db name after mongodb.net/<project-name>? in the connection string.
  • Now create a new folder config and inside it a new file db.js. In this file, we'll write the code to connect with mongoose.


const mongoose = require('mongoose');

const connectDB = async () => {
    try {
        const conn = await mongoose.connect(process.env.MONGO_URI);
        console.log(`Mongo db connected: ${conn.connection.host}`);
    } catch (error) {
        console.log(error);
        process.exit(1);
    }
};

module.exports = connectDB;


Enter fullscreen mode Exit fullscreen mode

and add these lines to server.js file.



const connectDB = require('./config/db');

connectDB();
const app = express();
...


Enter fullscreen mode Exit fullscreen mode

Lets run the server, and you'll see that DB is connected.

db-connected-terminal

In the next blog, I'll create a todos model and update the controller methods.

Thank you for reading!
Feel free to connect on Twitter

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .