DEV Community

Cover image for Create MongoDB database and connect with Mongoose
Aneeqa Khan
Aneeqa Khan

Posted on • Updated on

Create MongoDB database and connect with Mongoose

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.


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


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


  • 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<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: ${}`);
    } catch (error) {

module.exports = connectDB;
Enter fullscreen mode Exit fullscreen mode

and add these lines to server.js file.

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

const app = express();
Enter fullscreen mode Exit fullscreen mode

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


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

Top comments (4)

diegoaf profile image
Diego Alves

How can take upload of images and acess link image and save to database? I have these question in my head

johongirr profile image

You can only save the url of the image in the database. Image itself can either be saves in the server files or on tools like Cloudinary which is very easy to set up

luiskendelick profile image
Luís Fernando Arnoni Fritzen

But we can do this by parsing the photo to base64, no?

pablofsix profile image
Juan Pablo Torres

Hi, I know that you need to create an schema to send data to mongo like this "mongoose.model('myCollection', SchemaName)". But the name of the collection in mongo is only with lowercase. I like to put names like "testUsers" for example but I´cant. Suggestions?? Thanks