Create MongoDB database and connect with Mongoose

Table Of Contents

Create MongoDB Database
Build a Database Cluster
Connect with Mongoose

Create MongoDB Database

First, go to the MongoDB site and signup. There is a signup from google or GitHub option as well.
After signup, c…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Aneeqa Khan

Table Of Contents

  • Create MongoDB Database
  • Build a Database Cluster
  • Connect with Mongoose

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;

and add these lines to server.js file.

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

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

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


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Aneeqa Khan


Print Share Comment Cite Upload Translate Updates
APA

Aneeqa Khan | Sciencx (2022-12-16T14:58:48+00:00) Create MongoDB database and connect with Mongoose. Retrieved from https://www.scien.cx/2022/12/16/create-mongodb-database-and-connect-with-mongoose/

MLA
" » Create MongoDB database and connect with Mongoose." Aneeqa Khan | Sciencx - Friday December 16, 2022, https://www.scien.cx/2022/12/16/create-mongodb-database-and-connect-with-mongoose/
HARVARD
Aneeqa Khan | Sciencx Friday December 16, 2022 » Create MongoDB database and connect with Mongoose., viewed ,<https://www.scien.cx/2022/12/16/create-mongodb-database-and-connect-with-mongoose/>
VANCOUVER
Aneeqa Khan | Sciencx - » Create MongoDB database and connect with Mongoose. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/12/16/create-mongodb-database-and-connect-with-mongoose/
CHICAGO
" » Create MongoDB database and connect with Mongoose." Aneeqa Khan | Sciencx - Accessed . https://www.scien.cx/2022/12/16/create-mongodb-database-and-connect-with-mongoose/
IEEE
" » Create MongoDB database and connect with Mongoose." Aneeqa Khan | Sciencx [Online]. Available: https://www.scien.cx/2022/12/16/create-mongodb-database-and-connect-with-mongoose/. [Accessed: ]
rf:citation
» Create MongoDB database and connect with Mongoose | Aneeqa Khan | Sciencx | https://www.scien.cx/2022/12/16/create-mongodb-database-and-connect-with-mongoose/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.