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.
- Click Create an organization button.
- Type your organization name and select
MongoDB Atlas
and press next. - You don't need to add any members and permissions. Just click Create organization button.
Now we need to create a project.
- Click New project button.
- Type name of project
todos
and click next. - 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
andpassword
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.
- Go to the
Collections
tab and press Add my own data button. - Type todosapp as
database name
and todos ascollection 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 theMONOGO_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 filedb.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.
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
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.