Where Are We Storing Stuff?
Before we code our full-stack MERN (MongoDB, Express, React, and Node.js) application, we need a place to store our data. The best way to do this is to set up a MongoDB instance. We’ll run MongoDB inside a Docker container to keep things simple and easily manageable.
Setting Up MongoDB with Docker
We'll use the official MongoDB image from Docker Hub. To pull and run the MongoDB container, execute the following command in your terminal:
`docker run -d --name mongodb -p 27000:27017 \
-e MONGO_INITDB_ROOT_USERNAME=watson \
-e MONGO_INITDB_ROOT_PASSWORD=watson mongo`
Let's break this down:
- docker run -d runs the container in detached mode (in the background).
- --name mongodb names the container "mongodb" for easy reference.
- -p 27000:27017 maps port 27000 on your local machine to MongoDB's default port 27017.
- -e MONGO_INITDB_ROOT_USERNAME=watson sets the MongoDB root username to "watson".
- -e MONGO_INITDB_ROOT_PASSWORD=watson sets the password to "watson".
- mongo specifies the image to use (Docker will pull it if it’s not already available locally).
- Once this command runs successfully, you’ll have a MongoDB instance up and running in a Docker container!
Connecting to MongoDB
Now that we have our MongoDB instance running, we need a way to interact with it. One of the best tools for this is Studio 3T for MongoDB. It provides an intuitive UI to visualize and manage your database.
To connect Studio 3T to your MongoDB instance:
- Download and install Studio 3T (there’s a free tier that works great).
- Open the application and create a New Connection.
- Create a new connection
- Past the connection string into the connection as follows: mongodb://watson:watson@localhost:27000/
- Click Test Connection to ensure everything works.
- Save and connect!
What's Next?
With our database set up, we’re ready to start building the backend of our application using Express.js. In the next part of this series, we’ll:
Set up a Node.js server
Connect it to our MongoDB database
Create our first API endpoints
Stay tuned for Part 2: Building the Backend with Express.js!
Top comments (0)